QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 878|回复: 0

[C#] C#使用 System.Net.Mail 发送邮件功能

[复制链接]

等级头衔

积分成就    金币 : 2804
   泡泡 : 1516
   精华 : 6
   在线时间 : 1243 小时
   最后登录 : 2024-5-2

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2023-1-23 13:54:13 | 显示全部楼层 |阅读模式
一、介绍+ v' G6 v& w) ?" s0 r9 i
       System.Net.Mail命名空间是在.NET Framework中新增的,该命名空间提供了发送电子邮件的功能。通过对本章的学习,读者可以轻松地使用.NET Framework提供的类库来发送电子邮件。System.Net.Mail 命名空间包含用于将电子邮件发送到SMTP服务器的类,这些类需要结合Microsoft SMTP Server一起使用。
: F/ n1 M" Z, p7 Q/ F: J6 S' p       System.Net.Mail 命名空间下有SmtpClient类用于发送邮件,可以完全代替SmtpMail类。利用SmtpClient类的Send方法可以完成发送电子邮件的传 输,也可以用SendAsync方法进行异步发送,后者发送完成后会产生一个SendCompleted 事件来通知发送结果。Send方法可以接受MailMessage类的对象作为参数。通过MailMessage类可以设置邮件更多的内容和格式,例如,为 Attachment类设置添加附件的参数。
7 j2 [; k( X3 N, p       SmtpClient 类与SMTP结合在一起,通过MailMessage类、MailAddress类、Attachment类来丰富电子邮件的内容和设置。图18-2展示 了用户通过System.Net.Mail命名空间下的类结合SMTP发送电子邮件的过程。0 i! h. P+ V+ n. R
二、SmtpClient类的语法定义如下:( m' o" l" b$ m/ \
public class SmtpClient. t/ W4 T/ q* q! I6 K( I+ u- ?9 S
下面的代码演示如何创 建一个SmtpClient的实例。
$ g( p9 U. q9 @9 K+ KSmtpClient  client = new SmtpClient (“smtp.Sina.com”); //直接通过构造函数设置SMTP 主机服务器0 o* ]5 u$ a6 _9 Z0 a& T
或:5 P1 H$ T+ W  S) e! Z
SmtpClient  client = new SmtpClient ();* e5 I# ^% G% j) F* i$ b7 K8 ~, i
Client. Host =” smtp.Sina.com”; //通过Host属性来设置SMTP 主机服务器+ \+ K* q, f+ O4 e
三、完整代码
- {) g( E; Y+ i- Y* E1 X0 P# G* P
  1.   /// <summary>
  2.     /// 邮件处理器
  3.     /// </summary>
  4.     public class MailHandler
  5.     {
  6.         private MailMessage _mailMessage;
  7.         private string _host;
  8.         private string _userName;
  9.         private string _password;
  10.         public MailHandler()
  11.         {
  12.         }
  13.         /// <summary>
  14.         /// 设置邮件信息
  15.         /// </summary>
  16.         /// <param name="subject">主体</param>
  17.         /// <param name="body">内容</param>
  18.         /// <param name="from">发件人</param>
  19.         /// <param name="to">收件人</param>
  20.         /// <param name="cc">抄送人</param>
  21.         /// <param name="bcc">密件抄送人</param>
  22.         /// <param name="isBodyHtml">内容是否为Html</param>
  23.         public void SetMailMessage(string subject, string body, string from, string[] to, string[] cc, string[] bcc, bool isBodyHtml = true)
  24.         {
  25.             _mailMessage = new MailMessage();
  26.             _mailMessage.Subject = subject;
  27.             _mailMessage.Body = body;
  28.             _mailMessage.IsBodyHtml = isBodyHtml;
  29.             _mailMessage.From = new MailAddress(from);
  30.             if (to != null)
  31.             {
  32.                 foreach (var item in to)
  33.                 {
  34.                     _mailMessage.To.Add(item);
  35.                 }
  36.             }
  37.             if (cc != null)
  38.             {
  39.                 foreach (var item in cc)
  40.                 {
  41.                     _mailMessage.CC.Add(item);
  42.                 }
  43.             }
  44.             if (bcc != null)
  45.             {
  46.                 foreach (var item in bcc)
  47.                 {
  48.                     _mailMessage.Bcc.Add(item);
  49.                 }
  50.             }
  51.             _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
  52.         }
  53.         /// <summary>
  54.         /// 配置Smtp服务主机及身份验证
  55.         /// </summary>
  56.         /// <param name="host">Smtp主机名或Ip</param>
  57.         /// <param name="userName">用户名</param>
  58.         /// <param name="password">密码</param>
  59.         public void SetSmtp(string host, string userName, string password)
  60.         {
  61.             this._host = host;
  62.             this._userName = userName;
  63.             this._password = password;
  64.         }
  65.         /// <summary>
  66.         /// 发送邮件
  67.         /// </summary>
  68.         public void Send()
  69.         {
  70.             using (var sc = new SmtpClient())
  71.             {
  72.                 sc.Host = _host;
  73.                 sc.Port = 25;
  74.                 sc.DeliveryMethod = SmtpDeliveryMethod.Network;
  75.                 sc.Credentials = new System.Net.NetworkCredential(_userName, _password);
  76.                 sc.Send(_mailMessage);
  77.             }
  78.         }
  79.         public string SendMail(string title, string content)
  80.         {
  81.             var smptHost = ConfigHelper.GetAppSetting("SmtpHost");
  82.             var userName = ConfigHelper.GetAppSetting("MailUserName");
  83.             var password = ConfigHelper.GetAppSetting("MailPassword");
  84.             var mailToAddress = ConfigHelper.GetAppSetting("MailAddress").Split(',');
  85.             if (string.IsNullOrWhiteSpace(smptHost))
  86.             {
  87.                 return "SmtpHost为空";
  88.             }
  89.             if (string.IsNullOrWhiteSpace(userName))
  90.             {
  91.                 return "发件人为空";
  92.             }
  93.             if (string.IsNullOrWhiteSpace(password))
  94.             {
  95.                 return "发件人密码为空";
  96.             }
  97.             if (mailToAddress.Length == 0)
  98.             {
  99.                 return "收件人列表为空";
  100.             }
  101.             var mailContent = @"<html><head><title>邮件内容</title></head>
  102.                                 <body>" + content + "</body></html>";
  103.             SetSmtp(smptHost, userName, password);
  104.             SetMailMessage(title, mailContent, userName, mailToAddress, null, null);
  105.             try
  106.             {
  107.                 Send();
  108.             }
  109.             catch (Exception ex)
  110.             {
  111.                 return ex.Message;
  112.             }
  113.             return null;
  114.         }
  115.     }
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-4 04:42

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表