在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文将介绍如何利用ASP.NET进行邮件的发送,包括三种类型:发送简单的文件,发送包含HTML格式的文件和发送包含附件的邮件。 Introduction Before We Start After installation, Right-click it, go into "Properties", the "Access" tab, and click the "Relay" button. With the "only the list below" radio button selected, you should see the local IP address: 127.0.0.1, if it's not there, you need to add it. Otherwise, the code will not work. Also, when using the code, locate the line where I am setting the SmtpServer. I'll be setting it to "localhost", but you'll need to replace it with the name or IP address of your SMTP server. If you are using a Windows Desktop Computer, then there is no need to change it. Send simple e-mail Dim msg As New Net.Mail.MailMessage( _
Sending E-mail Attachment Dim msg As New Net.Mail.MailMessage( _ All you need to do is check if the user posted a file or not, and if he did, then you need to create a new Net.Mail.Attachment object and add that to the MailMessage Object's Attachments collection. The line of code doing that could be broken down into the following, for simplicity; Dim strName As String = fileUpload.PostedFile.FileName Moving forward, let us now look at how to send an HTML e-mail. Send HTML E-Mail The major part is in choosing the editor to create HTML Messages. Free TextBox (http://freetextbox.com/) is one of the best free ASP.Net editor control out there, and that is what I'll be using in my sample. Before you proceed, please download the latest version of the text-Box from the site. The best thing about this Free TextBox is that there are no external Scripts to worry about. All JavaScripts are internally stored in the assembly. Add the assembly to your ToolBox, and replace the simple textbox with this editor. You'll access the editor the same way you accessed a simple text-box. (By using the Text Property) You'll just have to add one line of code, and that to set the MailMessage's IsHTMLBody property to true. Otherwise, it will not identify the text as Html. Run the application, and quickly write down some text into the editor.
Potentially Dangerous Request Error when sending e-mail The .NET framework is throwing up an error because it detected something in the entered text which looks like an HTML statement. The text doesn't need to contain valid HTML, just anything with opening and closing angled brackets ("<...>"). The reason behind the error is as a security precaution. Developers need to be aware that users might try to inject HTML (or even a script) into a text box which may affect how the form is rendered. For further details see http://www.asp.net/faq/RequestValidation.aspx. This checking was not performed in the .NET 1.0 framework and was introduced with the .NET 1.1 framework. To disable request validation on a page add the following directive to the existing "page" directive in the file - you will need to switch to the HTML view for this: ValidateRequest="false" For example if you already have: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyForm.aspx.vb" Inherits="Proj.MyForm"%> Then this should become: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyForm.aspx.vb" Inherits="Proj.MyForm" validateRequest="false"%> Alternately, you can globally turn request validation off. To do so, add the following to your web.config file: <pages validateRequest="false" /> This should go within the <system.web> section, resulting in turning of Request Validation for every page in the web application. End Result
本文源代码下载: |
请发表评论