博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
邮件发送助手类 0.10
阅读量:6810 次
发布时间:2019-06-26

本文共 4609 字,大约阅读时间需要 15 分钟。

1:  // --------------------------------------------------------------------------------------------------------------------
2:  // 
3:  //     Copyright © 2014 Wedn.Net. All Rights Reserved.
4:  // 
5:  // 
6:  //   邮件发送助手类  0.10
7:  //   Verion:0.10
8:  //   Description:通过SMTP发送邮件
9:  // 
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:      /// 
19:      /// 邮件发送助手类
20:      /// 
21:      /// 
22:      ///  2013-11-18 18:56 Created By iceStone
23:      /// 
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:          /// 
34:          /// 发送邮件到指定收件人
35:          /// 
36:          /// 
37:          ///  2013-11-18 18:55 Created By iceStone
38:          /// 
39:          /// 收件人地址
40:          /// 主题
41:          /// 正文内容(支持HTML)
42:          /// 抄送地址列表
43:          /// 
是否发送成功
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:          /// 
50:          /// 发送邮件到指定收件人
51:          /// 
52:          /// 
53:          ///  2013-11-18 18:55 Created By iceStone
54:          /// 
55:          /// 收件人地址列表
56:          /// 主题
57:          /// 正文内容(支持HTML)
58:          /// 抄送地址列表
59:          /// 密件抄送地址列表
60:          /// 此邮件的优先级
61:          /// 附件列表
62:          /// 
是否发送成功
63:          /// 
attachments
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

你可能感兴趣的文章
《maven实战》学习笔记7——maven项目版本管理和灵活构建
查看>>
嵌入式 linux 查看内存
查看>>
mysql 协议的server状态标识
查看>>
CSDN去广告小脚本
查看>>
辟谣!Java 9使用指南10大误解,你中了几条?
查看>>
KSQL,用于Apache Kafka的流数据SQL引擎
查看>>
Zeppelin对Spark进行交互式数据查询和分析
查看>>
漂亮的后台界面PSD下载
查看>>
REST_FRAMEWORK加深记忆-第二次练习官方文档2
查看>>
NGINX配置小随笔
查看>>
大数据能帮企业抓住网络入侵者吗?
查看>>
BoCloud博云完成近亿元融资,加速PaaS与云运维落地
查看>>
IEEE:全球超一半大公司正在研究区块链,但是你需要区块链吗?
查看>>
与线性代数相关的数学词汇
查看>>
托管统一通信 向“云计算”迁移
查看>>
[译] 理解 NodeJS 中基于事件驱动的架构
查看>>
新计算,新纪元——2017 Kubertenes Meetup 即将开幕!
查看>>
【操作系统】3、存储管理
查看>>
咪咕视讯王斌:5G时代的泛娱乐产业生长
查看>>
VC中的正则表达式使用
查看>>