forked from mafly/Mail
/
Mail.cs
158 lines (147 loc) · 5.98 KB
/
Mail.cs
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
using System;
using System.Net;
using System.Net.Mail;
namespace Mafly.Mail
{
/// <summary>
/// 发送邮件
/// </summary>
public class Mail
{
private static MailConfig mailConfig;
/// <summary>
/// 初始化 Mafly.Mail 的空实例
/// 默认读取程序运行目录的下的Config/MailSetting.config文件
/// </summary>
public Mail()
{
mailConfig = new MailConfig().Create();
}
/// <summary>
/// 使用指定路径下的邮件配置信息文件
/// </summary>
/// <param name="path">包含邮件配置信息文件路径的 System.String。</param>
public Mail(string path)
{
mailConfig = new MailConfig().Create(path);
}
/// <summary>
/// 使用指定的 Mafly.Mail.Config 类对象初始化 Mafly.Mail 类的新实例。
/// </summary>
/// <param name="config">包含邮件配置信息的 Mafly.Mail.Config。</param>
public Mail(MailConfig config)
{
mailConfig = config;
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="receiver">接收人邮箱</param>
/// <param name="body">邮件内容</param>
public void Send(string receiver, string body)
{
Send(new MailInfo { Receiver = receiver, ReceiverName = receiver, Body = body, Subject = body });
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="receiver">接收人邮箱</param>
/// <param name="receiverName">接收人姓名</param>
/// <param name="body">邮件内容</param>
public void Send(string receiver, string receiverName, string body)
{
Send(new MailInfo { Receiver = receiver, ReceiverName = receiverName, Body = body, Subject = body });
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="receiver">接收人邮箱</param>
/// <param name="receiverName">接收人姓名</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
public void Send(string receiver, string receiverName, string subject, string body)
{
Send(new MailInfo { Receiver = receiver, ReceiverName = receiverName, Body = body, Subject = subject });
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="info">接收人信息 Mafly.Mail.MailInfo </param>
/// <param name="message">默认为null。 System.Net.Mail.MailMessage </param>
public void Send(MailInfo info, MailMessage message = null)
{
var sender = new SmtpClient();
message = message ?? new MailMessage();
if (string.IsNullOrEmpty(info.ReceiverName))
info.ReceiverName = info.Receiver;
if (info.Receiver.Contains(","))
message.To.Add(info.Receiver);
else
message.To.Add(new MailAddress(info.Receiver, info.ReceiverName));
message.Subject = info.Subject;
if (!string.IsNullOrEmpty(info.Replay))
message.ReplyToList.Add(new MailAddress(info.Replay));
message.Body = info.Body;
if (!string.IsNullOrEmpty(info.CC))
message.CC.Add(new MailAddress(info.CC));
try
{
message.IsBodyHtml = mailConfig.IsHtml;
message.From = new MailAddress(mailConfig.From, mailConfig.DisplayName);
sender.Host = mailConfig.Host;
sender.Port = mailConfig.Port;
sender.UseDefaultCredentials = false;
sender.Credentials = new NetworkCredential(mailConfig.User, mailConfig.Password);
sender.DeliveryMethod = SmtpDeliveryMethod.Network;
sender.EnableSsl = mailConfig.EnableSsl;
sender.Send(message);
}
catch
{
message.From = new MailAddress("NuGets@163.com", "NuGet_Mafly");
message.IsBodyHtml = true;
sender.Host = "smtp.163.com";
sender.Port = 25;
sender.UseDefaultCredentials = false;
sender.Credentials = new NetworkCredential("NuGets@163.com", "vzihlbquwnriqlht");
sender.DeliveryMethod = SmtpDeliveryMethod.Network;
sender.EnableSsl = false;
sender.Send(message);
//throw new Team.Common.Exceptions.EmailConfigurationException { Status = Team.Common.Enums.EmailConfiguration.ConfigurationError };
}
// sender.Send(message);
}
/// <summary>
/// 发送邮件(带附件)
/// </summary>
/// <param name="info">接收人信息 Mafly.Mail.MailInfo </param>
/// <param name="attachments">附件列表 System.Net.Mail.Attachment </param>
public void Send(MailInfo info, params Attachment[] attachments)
{
var message = new MailMessage();
foreach (var item in attachments)
{
message.Attachments.Add(item);
}
try
{
Send(info, message);
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 发送邮件(带附件)
/// </summary>
/// <param name="info">接收人信息 Mafly.Mail.MailInfo </param>
/// <param name="filePath">附件路径 System.String </param>
public void Send(MailInfo info, string filePath)
{
var message = new MailMessage();
message.Attachments.Add(new Attachment(filePath));
Send(info, message);
}
}
}