关于C#中发送电子邮件的说明

Microsoft .Net FrameWork提供的System.Net.Mail可以轻松实现电子邮件的发送:

// 发件箱地址
string emailFrom = "from@email.com";
// 收件箱地址
string emailTo = "to@email.com";
MailMessage mailMessage = new MailMessage();
MailAddress addressFrom = new MailAddress(emailFrom, "from", Encoding.UTF8);
MailAddress addressTo = new MailAddress(emailTo, "to", Encoding.UTF8);
mailMessage.From = addressFrom; 
mailMessage.To.Add(addressTo);
// 添加邮件附件
Attachment attachment = new Attachment("email.txt");
mailMessage.Attachments.Add(attachment);
// 邮件编码
mailMessage.SubjectEncoding = Encoding.UTF8;
mailMessage.BodyEncoding = Encoding.UTF8;
// 邮件主题
mailMessage.Subject = "title";
// 邮件内容
mailMessage.Body = "content";
// 是否是HTML邮件
mailMessage.IsBodyHtml = false; 
// 邮件优先级
mailMessage.Priority = MailPriority.High;
// 邮件SMTP服务器参数
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.email.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(emailFrom, "password");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
// 发送邮件
object userToken = mailMessage;
smtpClient.SendAsync(mailMessage, userToken);

发表评论