EmailHelper.cs
5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace Hh.Mes.Common.Http
{
public class EmailHelper
{
//var email = new EmailHelper()
//{
// MailFrom = "xxx@qq.com",
// //密码或者qq 授权码 隔一段时间可能会变化
// MailPwd = "cexkbsvbfcsibjaj",
// Host = "smtp.qq.com",
// Nickname = "【发邮件人昵称】我是系统管理员",
// MailSubject = "【邮件标题】系统提示",
// MailBody = "【邮件内容】今天你吃饭了吗?回答:今天下雨了,气温25度",
// MailToArray = new string[] { "p6417562@163.com" }
//};
#region Eail 属性
/// <summary>
/// 发送者的邮箱
/// </summary>
public string MailFrom { get; set; } = "system@haojima.net";
/// <summary>
/// 收件人
/// </summary>
public string[] MailToArray { get; set; }
/// <summary>
/// 抄送
/// </summary>
public string[] MailCcArray { get; set; }
/// <summary>
/// 邮箱标题
/// </summary>
public string MailSubject { get; set; }
/// <summary>
/// 邮箱正文
/// </summary>
public string MailBody { get; set; }
/// <summary>
/// 发送者的邮箱 密码
/// 直接使用密码可能会提示 不允许使用邮箱名称。 服务器响应为: xxxw
/// 要使用邮件授权码有安全机制 https://www.cnblogs.com/SharePointFans/p/5187504.html
/// </summary>
public string MailPwd { get; set; }
/// <summary>
/// 发送者的邮箱 SMTP邮件服务器
/// 163:smtp.163.com,
/// qq: smtp.qq.com
/// </summary>
public string Host { get; set; } = "smtp.haojima.net";
/// <summary>
/// 正文是否是html格式
/// </summary>
public bool IsBodyHtml { get; set; } = true;
/// <summary>
/// 发送者昵称
/// </summary>
public string Nickname { get; set; } = "";
/// <summary>
/// 附件
/// </summary>
public string[] AttachmentsPath { get; set; }
//优先级别
/// <summary>
/// 优先级别 默认正常优先级
/// </summary>
public MailPriority Priority { get; set; } = MailPriority.Normal;
#endregion
/// <summary>
/// Item1 为true 发送成功, Item2:返回信息
/// </summary>
/// <returns></returns>
public Tuple<bool, string> Send()
{
var myMail = InitMailAddress();
#region 在有附件的情况下添加附件
try
{
if (AttachmentsPath != null && AttachmentsPath.Length > 0)
{
Attachment attachFile = null;
foreach (string path in AttachmentsPath)
{
attachFile = new Attachment(path);
myMail.Attachments.Add(attachFile);
}
}
}
catch (Exception err)
{
return new Tuple<bool, string>(false, "在添加附件时有错误:" + err);
}
#endregion
return SendEmailMethod(myMail);
}
private MailMessage InitMailAddress()
{
//使用指定的邮件地址初始化MailAddress实例
var maddr = new MailAddress(MailFrom, Nickname);
//初始化MailMessage实例
var myMail = new MailMessage();
//向收件人地址集合添加邮件地址
if (MailToArray != null)
{
for (int i = 0; i < MailToArray.Length; i++)
{
myMail.To.Add(MailToArray[i].ToString());
}
}
//向抄送收件人地址集合添加邮件地址
if (MailCcArray != null)
{
for (int i = 0; i < MailCcArray.Length; i++)
{
myMail.CC.Add(MailCcArray[i].ToString());
}
}
//发件人地址
myMail.From = maddr;
//电子邮件的标题
myMail.Subject = MailSubject;
//电子邮件的主题内容使用的编码
myMail.SubjectEncoding = Encoding.UTF8;
//电子邮件正文
myMail.Body = MailBody;
//电子邮件正文的编码
myMail.BodyEncoding = Encoding.Default;
//邮件优先级
myMail.Priority = Priority;
myMail.IsBodyHtml = IsBodyHtml;
return myMail;
}
private Tuple<bool, string> SendEmailMethod(MailMessage myMail)
{
var smtp = new SmtpClient
{
//指定发件人的邮件地址和密码以验证发件人身份
Credentials = new NetworkCredential(MailFrom, MailPwd), //115 //设置SMTP邮件服务器
Host = Host
};
// smtp.EnableSsl = true;
//smtp.Port = 587;
try
{
//将邮件发送到SMTP邮件服务器
smtp.Send(myMail);
return new Tuple<bool, string>(true, "邮件发送成功,请稍后查看收件邮件,邮件可能会延迟,请耐心等待!");
}
catch (SmtpException ex)
{
return new Tuple<bool, string>(false, "邮件发送失败!" + ex);
}
}
}
}