<mailSettings>
<smtp>
<network host="localhost" port="25" userName="your username"
password="your password"/>
</smtp>
</mailSettings>
</system.net>
发送简单的文本格式的邮件
| <%@ Page Language="C#" %> <%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.Net.Mail" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void SendEmail_Click (object sender, EventArgs e) { MailMessage mm = new MailMessage (tbxUsersEmail.Text, tbxUsersEmail.Text); mm.Subject = tbxSubject.Text; mm.Body = tbxBody.Text; mm.IsBodyHtml = false; SmtpClient smtp = new SmtpClient (); smtp.Send (mm); Response.Write ("Completed!"); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>SendMail</title> </head> <body> <form /> </form> </body> </html> |
发送 HTML 格式的邮件
| 1 <%@ Page Language="C#" %> 2 <%@ Import Namespace="System.Net" %> 3 <%@ Import Namespace="System.Net.Mail" %> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 7 <script runat="server"> 8 protected void Page_Load (object sender, EventArgs e) 9 { 10 lblContent.Text = @"<h2>This is an HTML-Formatted Email Send Using the <code>IsBodyHtml</code> Property</h2><p>Isn’t HTML <em>neat</em>?</p> <p>You can make all sorts of <span style=""color:red;font-weight:bold;""> pretty colors!!</span>.</p>"; 11 } 12 13 protected void SendMail_Click (object sender, EventArgs e) 14 { 15 MailMessage mm = new MailMessage (tbxMail.Text, tbxMail.Text); 16 17 mm.Subject = "HTML-Formatted Email test"; 18 mm.Body = lblContent.Text; 19 mm.IsBodyHtml = true; 20 21 SmtpClient smtp = new SmtpClient (); 22 23 smtp.Send (mm); 24 Response.Write ("Completed!"); 25 } 26 </script> 27 28 <html xmlns="http://www.w3.org/1999/xhtml" > 29 <head runat="server"> 30 <title>SendHtmlMail</title> 31 </head> 32 <body> 33 <form /> 43 </div> 44 </form> 45 </body> 46 </html> 47 |
发送带有上传附件的邮件
| 1 <%@ Page Language="C#" %> 2 <%@ Import Namespace="System.Net" %> 3 <%@ Import Namespace="System.Net.Mail" %> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 7 <script runat="server"> 8 protected void SendMail_Click (object sender, EventArgs e) 9 { 10 MailMessage mm = new MailMessage (tbxUsersEmail.Text, tbxUsersEmail.Text); 11 12 mm.IsBodyHtml = false; 13 mm.Subject = "Emailing an Uploaded File as an Attachment Demo"; 14 mm.Body = tbxBody.Text; 15 mm.Attachments.Add ( new Attachment (fuAttachmentFile.PostedFile.InputStream, fuAttachmentFile.FileName)); 16 17 SmtpClient smtp = new SmtpClient (); 18 19 smtp.Send (mm); 20 Response.Write ("Completed!"); 21 } 22 </script> 23 24 <html xmlns="http://www.w3.org/1999/xhtml" > 25 <head runat="server"> 26 <title>Send mail with attachment file.</title> 27 </head> 28 <body> 29 <form /> 60 </form> 61 </body> 62 </html> |