使用System.Web.Mail发送E-mail
要发送E-mail必须引入命名空间System.Web.Mail,使用其中的MailMessage和SmtpMail对象,其中MailMessage用来设定E-mail的内容,而SmtpMail则负责把E-mail发出去。
protected void Page_Load(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
//收件人地址
mail.To = "paopao@dongfeng.net";
//发件人地址
mail.From = "paopao@dongfeng.net";
mail.Subject = "邮件标题";
mail.Body = "邮件内容";
//设置需要服务器端验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//验证用户名
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
//验证密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");
//发送邮件的SMTP服务器地址
SmtpMail.SmtpServer = "SMTP服务器地址";
SmtpMail.Send(mail);
Response.Write("发送成功!");
}
{
MailMessage mail = new MailMessage();
//收件人地址
mail.To = "paopao@dongfeng.net";
//发件人地址
mail.From = "paopao@dongfeng.net";
mail.Subject = "邮件标题";
mail.Body = "邮件内容";
//设置需要服务器端验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//验证用户名
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
//验证密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");
//发送邮件的SMTP服务器地址
SmtpMail.SmtpServer = "SMTP服务器地址";
SmtpMail.Send(mail);
Response.Write("发送成功!");
}