C# 邮件发送

2021-08-14 20:58

阅读:783

标签:setting   2-2   命名空间   ntp   邮箱   参考   加密   man   sendemail   1.创建控制台程序: 2.添加一个与发送邮件相关的实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SendEmail { /// /// 邮件发送实体类 /// public class EmailSend { /// /// 邮件服务器 /// public string smtpServer { get; set; } /// /// 发件邮箱 /// public string sendEmail { get; set; } /// /// 收件箱 /// public string dstEmail { get; set; } /// /// 授权码 /// public sbyte pwd { get; set; } /// /// 抄送邮箱 /// public string ccEmail { get; set; } /// /// 密送邮箱 /// public string bccEmail { get; set; } /// /// 邮件主题(标题) /// public string theme { get; set; } /// /// 邮件内容 /// public string content { get; set; } } } 3.添加一个配置文件App.config,将相关内容配置到里面: 4.程序入口: 引入命名空间: using System.Net.Mail;using System.Net;   using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Timers; using System.Net.Mail; using System.Net; namespace SendEmailDemo { class Program { static void Main(string[] args) { //创建一个邮件实体对象 EmailSend email = new EmailSend(); //从配置文件取出相关的值并对邮件实体对象赋值 //获取邮件服务器 email.smtpServer = System.Configuration.ConfigurationManager.AppSettings["smtpserver"]; //获取发件箱 email.sendEmail = System.Configuration.ConfigurationManager.AppSettings["sendEmail"]; //获取收件箱 email.dstEmail = System.Configuration.ConfigurationManager.AppSettings["dstEmail"]; //获取客户端授权码 email.clientPwd = System.Configuration.ConfigurationManager.AppSettings["clientPwd"]; //获取抄送邮箱 email.ccEmail = System.Configuration.ConfigurationManager.AppSettings["ccEmail"]; //获取密送邮箱 email.bccEmail = System.Configuration.ConfigurationManager.AppSettings["bccEmail"]; email.theme = "C#邮件发送测试"; email.content = "邮件测试内容"; string msg=SendEmail(email); Console.WriteLine(msg); Console.ReadKey(); } public static string SendEmail(EmailSend _mailSend) { try { MailMessage mail = new MailMessage(); //邮件服务器客户端对象 SmtpClient client = new SmtpClient(); //设置邮件服务器 client.Host = _mailSend.smtpServer; //设置端口 client.Port = 25; //是否使用安全套接字层加密连接 client.EnableSsl = false; //不使用默认凭证,注意此句必须放在 client.Credentials 的前面 client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(_mailSend.sendEmail, _mailSend.clientPwd); //邮件通过网络直接发送到服务器 client.DeliveryMethod = SmtpDeliveryMethod.Network; //账号信息 mail.From = new MailAddress(_mailSend.sendEmail); mail.To.Clear(); //添加收件箱 foreach (var item in _mailSend.dstEmail.Split(‘;‘)) { if (string.IsNullOrEmpty(item)) { continue; } mail.To.Add(item); } //添加抄送 if (!string.IsNullOrEmpty(_mailSend.ccEmail)) { foreach (var item in _mailSend.ccEmail.Split(‘;‘)) { if (string.IsNullOrEmpty(item)) { continue; } mail.CC.Add(new MailAddress(item)); } } //添加密送 if (!string.IsNullOrEmpty(_mailSend.bccEmail)) { foreach (var item in _mailSend.bccEmail.Split(‘;‘)) { if (string.IsNullOrEmpty(item)) { continue; } mail.Bcc.Add(new MailAddress(item)); } } //设置主题 mail.Subject = _mailSend.theme; //设置内容 mail.Body = _mailSend.content; //设置编码 mail.SubjectEncoding = Encoding.UTF8; //发送邮件 client.Send(mail); return "邮件发送成功!!"; } catch (Exception ex) { return"邮件发送异常,捕获异常:"+ex.Message; } } } }  5.未将邮箱的客户端授权开启时,会出现如下异常: 6.浏览器登录邮箱-->设置-->客户端授权密码-->点击开启-->输入验证码-->确定 7.设置授权码 8.设置成功,注:此授权码为客户端发送邮件所必须的 9.再次发送邮件: 10.浏览器登录邮箱查看:   成功!! 11.官网参考地址:https://msdn.microsoft.com/zh-cn/library/system.net.mail.mailmessage(VS.80).aspxC# 邮件发送标签:setting   2-2   命名空间   ntp   邮箱   参考   加密   man   sendemail   原文地址:http://www.cnblogs.com/jiangxianshen/p/7495111.html


评论


亲,登录后才可以留言!