邮件发送助手类 0.10

   1:  // --------------------------------------------------------------------------------------------------------------------
   2:  // <copyright file="MailHelper.cs" company="Wedn.Net">
   3:  //     Copyright © 2014 Wedn.Net. All Rights Reserved.
   4:  // </copyright>
   5:  // <summary>
   6:  //   邮件发送助手类  0.10
   7:  //   Verion:0.10
   8:  //   Description:通过SMTP发送邮件
   9:  // </summary>
  10:  // --------------------------------------------------------------------------------------------------------------------
  11:  namespace Micua.Infrastructure.Utility
  12:  {
  13:      using System;
  14:      using System.Linq;
  15:      using System.Net.Mail;
  16:      using System.Text;
  17:   
  18:      /// <summary>
  19:      /// 邮件发送助手类
  20:      /// </summary>
  21:      /// <remarks>
  22:      ///  2013-11-18 18:56 Created By iceStone
  23:      /// </remarks>
  24:      public static class MailHelper
  25:      {
  26:          private readonly static string SmtpServer = "smtp.wedn.net";
  27:          private readonly static int SmtpServerPort = 25;
  28:          private readonly static bool SmtpEnableSsl = false;
  29:          private readonly static string SmtpUsername = "server@wedn.net";
  30:          private readonly static string SmtpDisplayName = "Wedn.Net";
  31:          private readonly static string SmtpPassword = "2014@itcast";
  32:   
  33:          /// <summary>
  34:          /// 发送邮件到指定收件人
  35:          /// </summary>
  36:          /// <remarks>
  37:          ///  2013-11-18 18:55 Created By iceStone
  38:          /// </remarks>
  39:          /// <param name="to">收件人地址</param>
  40:          /// <param name="subject">主题</param>
  41:          /// <param name="mailBody">正文内容(支持HTML)</param>
  42:          /// <param name="copyTos">抄送地址列表</param>
  43:          /// <returns>是否发送成功</returns>
  44:          public static bool Send(string to, string subject, string mailBody, params string[] copyTos)
  45:          {
  46:              return Send(new[] { to }, subject, mailBody, copyTos, new string[] { }, MailPriority.Normal);
  47:          }
  48:   
  49:          /// <summary>
  50:          /// 发送邮件到指定收件人
  51:          /// </summary>
  52:          /// <remarks>
  53:          ///  2013-11-18 18:55 Created By iceStone
  54:          /// </remarks>
  55:          /// <param name="tos">收件人地址列表</param>
  56:          /// <param name="subject">主题</param>
  57:          /// <param name="mailBody">正文内容(支持HTML)</param>
  58:          /// <param name="ccs">抄送地址列表</param>
  59:          /// <param name="bccs">密件抄送地址列表</param>
  60:          /// <param name="priority">此邮件的优先级</param>
  61:          /// <param name="attachments">附件列表</param>
  62:          /// <returns>是否发送成功</returns>
  63:          /// <exception cref="System.ArgumentNullException">attachments</exception>
  64:          public static bool Send(string[] tos, string subject, string mailBody, string[] ccs, string[] bccs, MailPriority priority, params Attachment[] attachments)
  65:          {
  66:              if (attachments == null) throw new ArgumentNullException("attachments");
  67:              if (tos.Length == 0) return false;
  68:              //创建Email实体
  69:              var message = new MailMessage();
  70:              message.From = new MailAddress(SmtpUsername, SmtpDisplayName);
  71:              message.Subject = subject;
  72:              message.Body = mailBody;
  73:              message.BodyEncoding = Encoding.UTF8;
  74:              message.IsBodyHtml = true;
  75:              message.Priority = priority;
  76:              //插入附件
  77:              foreach (var attachment in attachments)
  78:              {
  79:                  message.Attachments.Add(attachment);
  80:              }
  81:              //插入收件人地址,抄送地址和密件抄送地址
  82:              foreach (var to in tos.Where(c => !string.IsNullOrEmpty(c)))
  83:              {
  84:                  message.To.Add(new MailAddress(to));
  85:              }
  86:              foreach (var cc in ccs.Where(c => !string.IsNullOrEmpty(c)))
  87:              {
  88:                  message.CC.Add(new MailAddress(cc));
  89:              }
  90:              foreach (var bcc in bccs.Where(c => !string.IsNullOrEmpty(c)))
  91:              {
  92:                  message.CC.Add(new MailAddress(bcc));
  93:              }
  94:              //创建SMTP客户端
  95:              var client = new SmtpClient
  96:              {
  97:                  Host = SmtpServer,
  98:                  Credentials = new System.Net.NetworkCredential(SmtpUsername, SmtpPassword),
  99:                  DeliveryMethod = SmtpDeliveryMethod.Network,
 100:                  EnableSsl = SmtpEnableSsl,
 101:                  Port = SmtpServerPort
 102:              };
 103:              //client.SendCompleted += Client_SendCompleted;
 104:              //try
 105:              //{
 106:              //发送邮件
 107:              client.Send(message);
 108:              //client.SendAsync(message,DateTime.Now.ToString());
 109:   
 110:              //client.Dispose();
 111:              //message.Dispose();
 112:              return true;
 113:              //}
 114:              //catch (Exception)
 115:              //{
 116:              //    throw;
 117:              //}
 118:          }
 119:      }
 120:  }

 

地图图片

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
博文来源广泛,如原作者认为我侵犯知识产权,请尽快给我发邮件 664507902@qq.com联系,我将以第一时间删除相关内容。

posted @ 2015-09-20 22:32  木头园—OOIP  阅读(174)  评论(0编辑  收藏  举报