c# 发邮件
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.IO; using System.Web; using System.Net.Mail; using System.Net; using System.ComponentModel; namespace EasyFrame.Common { public class GTMail { #region 发送电子邮件 /// <summary> /// 发送电子邮件 /// </summary> /// <param name="smtpserver">SMTP服务器</param> /// <param name="userName">登录帐号</param> /// <param name="pwd">登录密码</param> /// <param name="nickName">发件人昵称</param> /// <param name="strfrom">发件人</param> /// <param name="strto">收件人</param> /// <param name="subj">主题</param> /// <param name="bodys">内容</param> public static void sendMail(string smtpserver, string userName, string pwd, string nickName, string strfrom, string strto, string subj, string bodys) { SmtpClient _smtpClient = new SmtpClient(); _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 _smtpClient.Host = smtpserver;//指定SMTP服务器 _smtpClient.Credentials = new System.Net.NetworkCredential(userName, pwd);//用户名和密码 //MailMessage _mailMessage = new MailMessage(strfrom, strto); MailAddress _from = new MailAddress(strfrom, nickName); MailAddress _to = new MailAddress(strto); MailMessage _mailMessage = new MailMessage(_from, _to); _mailMessage.Subject = subj;//主题 _mailMessage.Body = bodys;//内容 _mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码 _mailMessage.IsBodyHtml = true;//设置为HTML格式 _mailMessage.Priority = MailPriority.Normal;//优先级 _smtpClient.Send(_mailMessage); } #endregion #region 简单电子邮件发送 /// <summary> /// 简单电子邮件发送 /// </summary> /// <param name="smtpserver">SMTP服务器</param> /// <param name="userName">登录帐号</param> /// <param name="pwd">登录密码</param> /// <param name="nickName">发件人昵称</param> /// <param name="mailFrom">发件人</param> /// <param name="mailTo">收件人</param> /// <param name="subj">主题</param> /// <param name="bodys">内容</param> public static void sendMail(string smtpserver, int smtpport, string userName, string pwd, string nickName, string mailFrom, string mailTo, string subj, string bodys) { SmtpClient _smtpClient = new SmtpClient(); _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 _smtpClient.Host = smtpserver;//指定SMTP服务器 _smtpClient.Port = smtpport; _smtpClient.EnableSsl = true; //_smtpClient.SendCompleted += new SendCompletedEventHandler(Smtp_SendCompleted); _smtpClient.Credentials = new NetworkCredential(userName, pwd);//用户名和密码 MailAddress _from = new MailAddress(mailFrom, nickName, Encoding.GetEncoding(936)); MailAddress _to = new MailAddress(mailTo); MailMessage _mailMessage = new MailMessage(_from, _to); _mailMessage.Subject = subj;//主题 _mailMessage.Body = bodys;//内容 _mailMessage.BodyEncoding = Encoding.Default;//正文编码 _mailMessage.IsBodyHtml = true;//设置为HTML格式 _mailMessage.Priority = MailPriority.Normal;//优先级 _smtpClient.Send(_mailMessage); //_smtpClient.SendMailAsync(_mailMessage); } private static void Smtp_SendCompleted(object sender, AsyncCompletedEventArgs e) { string msg = e.UserState.ToString(); SmtpClient smtp = (SmtpClient)sender; if (e.Error == null)//问题出现后,这里的Error并没有错误 { msg += ",1";//发送成功 } else { msg += ",0"; smtp.SendAsyncCancel(); } } #endregion #region 邮件批量发送 /// <summary> /// 邮件批量发送 /// </summary> /// <param name="smtpserver"></param> /// <param name="smtpport"></param> /// <param name="userName"></param> /// <param name="pwd"></param> /// <param name="mailFrom"></param> /// <param name="nickName"></param> /// <param name="mailTo"></param> /// <param name="subj"></param> /// <param name="bodys"></param> /// <param name="cc"></param> /// <param name="bcc"></param> /// <param name="mailAttachments"></param> public static void SendMail(string smtpserver, int smtpport, string userName, string pwd, string mailFrom, string nickName, string[] mailTo, string subj, string bodys, string[] cc, string[] bcc, string[] mailAttachments) { SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network smtp.EnableSsl = false;//smtp服务器是否启用SSL加密 smtp.Host = smtpserver; //指定 smtp 服务器地址 smtp.Port = smtpport; //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去 //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了 smtp.UseDefaultCredentials = true; //smtp.SendCompleted += new SendCompletedEventHandler(Smtp_SendCompleted); //如果需要认证,则用下面的方式 smtp.Credentials = new NetworkCredential(userName, pwd); MailMessage mm = new MailMessage(); //实例化一个邮件类 mm.Priority = MailPriority.Normal; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可 mm.From = new MailAddress(mailFrom, nickName, Encoding.GetEncoding(936)); //收件方看到的邮件来源; //第一个参数是发信人邮件地址 //第二参数是发信人显示的名称 //第三个参数是 第二个参数所使用的编码,如果指定不正确,则对方收到后显示乱码 //936是简体中文的codepage值 //注:上面的邮件来源,一定要和你登录邮箱的帐号一致,否则会认证失败 mm.ReplyToList.Add(mailFrom); //ReplyTo 表示对方回复邮件时默认的接收地址,即:你用一个邮箱发信,但却用另一个来收信 if (cc.Length > 0) { mm.CC.Add(string.Join(",", cc));//抄送 } if (bcc.Length > 0) { mm.Bcc.Add(string.Join(",", bcc));//密送 } if (mailAttachments.Length > 0) { for (int i = 0; i < mailAttachments.Length; i++) { Attachment objMailAttachment = new Attachment(@mailAttachments[i]);//发送邮件的附件的地址 mm.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中 } } mm.To.Add(string.Join(",", mailTo)); mm.Subject = subj; //邮件标题 mm.SubjectEncoding = Encoding.GetEncoding(936); // 这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。 // 936是简体中文的pagecode,如果是英文标题,这句可以忽略不用 mm.IsBodyHtml = true; //邮件正文是否是HTML格式 mm.BodyEncoding = Encoding.GetEncoding(936); //邮件正文的编码, 设置不正确, 接收者会收到乱码 mm.Body = bodys; //邮件正文 smtp.Send(mm); //smtp.SendMailAsync(mm); //发送邮件,如果不返回异常, 则大功告成了。 } #endregion } }
从此山高路远,纵马扬鞭。愿往后旅途,三冬暖,春不寒,天黑有灯,下雨有伞。此生尽兴,不负勇往。