System.Net.Mail.MailMessage 进行email发送
来源:互联网 发布:腹黑兔子无耻外交 知乎 编辑:程序博客网 时间:2024/04/27 14:32
从.NET 2.0 开始,引入了一个新的类,System.Net.Mail.MailMessage。该类用来取代 .NET 1.1 时代的 System.Web.Mail.MailMessage 类。
System.Net.Mail.MailMessage 类用于指定一个邮件,另外一个类 System.Net.Mail.SmtpClient 则用来设置 SMTP,然后发送邮件。由于目前 SMTP 都需要进行身份验证,有的还需要 SSL(比如GMail),所以设置的属性稍微多一些。代码片断如下:
using System.Net.Mail;
...
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress("你的email地址");
mailMsg.To.Add("接收人1的email地址");
mailMsg.To.Add("接收人2的email地址");
mailMsg.Subject = "邮件主题";
mailMsg.Body = "邮件主体内容";
mailMsg.BodyEncoding = Encoding.UTF8;
mailMsg.IsBodyHtml = false;
mailMsg.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient();
// 提供身份验证的用户名和密码
// 网易邮件用户可能为:username password
// Gmail 用户可能为:username@gmail.com password
smtp.Credentials = new NetworkCredential("用户名", "密码");
smtp.Port = 25; // Gmail 使用 465 和 587 端口
smtp.Host = "SMTP 服务器地址"; // 如 smtp.163.com, smtp.gmail.com
smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true
smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted);
try
{
smtp.SendAsync(mailMsg, mailMsg);
}
catch (SmtpException ex)
{
Console.WriteLine(ex.ToString());
}
...
void SendMailCompleted(object sender, AsyncCompletedEventArgs e)
{
MailMessage mailMsg = (MailMessage)e.UserState;
string subject = mailMsg.Subject;
if (e.Cancelled) // 邮件被取消
{
Console.WriteLine(subject + " 被取消。");
}
if (e.Error != null)
{
Console.WriteLine("错误:" + e.Error.ToString());
}
else
{
Console.WriteLine("发送完成。");
}
}
- System.Net.Mail.MailMessage 进行email发送
- 使用 System.Net.Mail.MailMessage 发送电子邮件
- 使用 System.Net.Mail.MailMessage 发送电子邮件
- 使用 System.Net.Mail.MailMessage 发送电子邮件
- 使用 System.Net.Mail.MailMessage 发送电子邮件
- System.Web.Mail.MailMessage邮件发送程序(asp.net)
- 用System.Net.Mail发送email
- 利用System.Net.Mail发送email
- 如何利用System.Net.Mail类发送EMAIL
- System.Net.Mail 发送邮件
- System.Net.Mail 邮件发送
- System.Net.Mail 发送邮件
- System.Net.Mail 发送邮件
- System.Net.Mail发送邮件
- System.Net.Mail 发送邮件
- asp.net 发送邮件 System.Net.Mail
- ASP.NET 邮件发送 System.Net.Mail
- Web上利用System.Web.Mail发送EMail
- 什么是通货紧缩和通货膨胀?其主要特征是什么〔转〕
- 高性能网站建设之减少Http连接数
- 好的测试工程师应具备的素质
- 层展开/关闭 运动缓冲效果
- JQuery复合属性选择器小例
- System.Net.Mail.MailMessage 进行email发送
- 通用分页存储过程
- PHP的拼写检查函数库
- JS显示或隐藏层的函数
- javascript 实现随机数
- Ubuntu 8.10环境NVIDIA显卡无法正常工作解决方法
- 关于PHP编码转化的问题
- J2SE基础复习提纲
- DBA2.0时代,DBA将何去何从