C#发送邮件及注意事项

        //参数配置
static
readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"]; static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"]; static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"]; static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]); static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"]; /// <summary> /// 发送邮件 /// </summary> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="email">接收人地址</param> public static void SendEmail(string subject, string body, string email) { try { List<string> toList = StringPlus.GetSubStringList(StringPlus.ToDBC(email), ','); OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(smtpServer, userName, pwd, smtpPort); foreach (string s in toList) { OpenSmtp.Mail.MailMessage msg = new OpenSmtp.Mail.MailMessage(); msg.From = new OpenSmtp.Mail.EmailAddress(userName, authorName); msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To); //设置邮件正文,并指定格式为 html 格式 msg.HtmlBody = body; //设置邮件标题 msg.Subject = subject; //指定邮件正文的编码 msg.Charset = "gb2312"; //发送邮件 smtp.SendMail(msg); } } catch (Exception ex) { } }

 

注意事项:

端口号是25

密码是授权码,非登录密码

posted @ 2019-01-09 10:28  张伟大  阅读(468)  评论(0编辑  收藏  举报