如何用System.Net.Mail来发送邮件
其实微软自带的 System.Net.Mail这个类用来发邮件是非常好用的.现做一个测试页面,具体过程如下:
1.新建一个网站,添加对System.Net的引用.
2.添加app_code 文件夹,在文件夹下添加sendmail.cs.
代码如下:
3.在IIS 中配置好SMTP服务器就可以了.
4.发送邮件测试代码:
1.新建一个网站,添加对System.Net的引用.
2.添加app_code 文件夹,在文件夹下添加sendmail.cs.
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
/// <summary>
/// Summary description for SendMail
/// </summary>
public class SendMail
{
/// <summary>
/// To send mail . Please pay attention to smtp host.
/// </summary>
/// <param name="from">sender</param>
/// <param name="to">receiver</param>
/// <param name="bcc">bcc to whom</param>
/// <param name="cc">cc to whom</param>
/// <param name="subject">subject of mail message</param>
/// <param name="body">body of mail message</param>
public static void SendMailProcess(string from, string to, string bcc, string cc, string subject, string body)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(from);
mailMessage.To.Add(to);
if(bcc!=null && bcc != string.Empty)
{
mailMessage.Bcc.Add(new MailAddress(bcc));
}
if(cc!=null && cc != string.Empty)
{
mailMessage.CC.Add(new MailAddress(cc));
}
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.Priority = MailPriority.Normal;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "localhost"; //change to your smtp host.
smtpClient.Send(mailMessage);
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
/// <summary>
/// Summary description for SendMail
/// </summary>
public class SendMail
{
/// <summary>
/// To send mail . Please pay attention to smtp host.
/// </summary>
/// <param name="from">sender</param>
/// <param name="to">receiver</param>
/// <param name="bcc">bcc to whom</param>
/// <param name="cc">cc to whom</param>
/// <param name="subject">subject of mail message</param>
/// <param name="body">body of mail message</param>
public static void SendMailProcess(string from, string to, string bcc, string cc, string subject, string body)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(from);
mailMessage.To.Add(to);
if(bcc!=null && bcc != string.Empty)
{
mailMessage.Bcc.Add(new MailAddress(bcc));
}
if(cc!=null && cc != string.Empty)
{
mailMessage.CC.Add(new MailAddress(cc));
}
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.Priority = MailPriority.Normal;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "localhost"; //change to your smtp host.
smtpClient.Send(mailMessage);
}
}
3.在IIS 中配置好SMTP服务器就可以了.
4.发送邮件测试代码:
string from = @"test@microsoft.com";
string to = @"test@microsoft.com";
string cc = @"test@microsoft.com";
SendMail.SendMailProcess(from, to, null, cc, "test", "just a test");
string to = @"test@microsoft.com";
string cc = @"test@microsoft.com";
SendMail.SendMailProcess(from, to, null, cc, "test", "just a test");
致力于SharePoint开发.
QQ:28748451.