邮件发送助手类 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:  }

 

地图图片

转载于:https://www.cnblogs.com/ooip/p/4769829.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值