实例一 MailHelper
代码如下 | 复制代码 |
using System; |
实例二SmtpClient
代码如下 | 复制代码 |
//创建smtpclient对象 System.Net.Mail.SmtpClient client = new SmtpClient(); client.Host = "smtp.163.com";//163的smtp服务器是 smtp.163.com string from = "***@163.com"; string pwd = "密码"; string tomail = "***@qq.com"; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(from, pwd); client.DeliveryMethod = SmtpDeliveryMethod.Network; System.Text.Encoding encoding = System.Text.Encoding.UTF8; string senderDisplayName = ConfigurationManager.AppSettings["senderDisplayName"];//这个配置的是发件人的要显示在邮件的名称 string recipientsDisplayName = "昊云";//这个配置的是收件人的要显示在邮件的名称 MailAddress mailfrom = new MailAddress(from, senderDisplayName, encoding);//发件人邮箱地址,名称,编码UTF8 MailAddress mailto = new MailAddress(tomail, recipientsDisplayName, encoding);//收件人邮箱地址,名称,编码UTF8 //创建mailMessage对象 System.Net.Mail.MailMessage message = new MailMessage(mailfrom, mailto); message.Subject = "测试邮件标题"; //正文默认格式为html message.Body = "测试邮件内容"; message.IsBodyHtml = true; message.BodyEncoding = encoding; message.SubjectEncoding = encoding; message.HeadersEncoding = encoding; client.Send(message); |