

1
public class MailSender
2
{
3
public static void Send(string server, string sender, string recipient, string subject,
4
string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
5
{
6
SmtpClient smtpClient = new SmtpClient(server);
7
MailMessage message = new MailMessage(sender, recipient);
8
message.IsBodyHtml = isBodyHtml;
9
10
message.SubjectEncoding = encoding;
11
message.BodyEncoding = encoding;
12
13
message.Subject = subject;
14
message.Body = body;
15
16
message.Attachments.Clear();
17
if (files != null && files.Length != 0)
18
{
19
for (int i = 0; i < files.Length; ++i)
20
{
21
Attachment attach = new Attachment(files[i]);
22
message.Attachments.Add(attach);
23
}
24
}
25
26
if (isAuthentication == true)
27
{
28
smtpClient.Credentials = new NetworkCredential(SmtpConfig.Create().SmtpSetting.User,
29
SmtpConfig.Create().SmtpSetting.Password);
30
}
31
smtpClient.Send(message);
32
33
34
}
35
36
public static void Send(string recipient, string subject, string body)
37
{
38
Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.Sender, recipient, subject, body, true, Encoding.Default, true, null);
39
}
40
41
public static void Send(string Recipient, string Sender, string Subject, string Body)
42
{
43
Send(SmtpConfig.Create().SmtpSetting.Server, Sender, Recipient, Subject, Body, true, Encoding.UTF8, true, null);
44
}
45
46
static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
47
static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
48
static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
49
static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
50
static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];
51
static readonly string to = System.Configuration.ConfigurationManager.AppSettings["To"];
52
53
54
public void Send(string subject, string body)
55
{
56
57
List<string> toList = StringPlus.GetSubStringList(StringPlus.ToDBC(to), ',');
58
OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(smtpServer, userName, pwd, smtpPort);
59
foreach (string s in toList)
60
{
61
OpenSmtp.Mail.MailMessage msg = new OpenSmtp.Mail.MailMessage();
62
msg.From = new OpenSmtp.Mail.EmailAddress(userName, authorName);
63
64
msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);
65
66
//设置邮件正文,并指定格式为 html 格式
67
msg.HtmlBody = body;
68
//设置邮件标题
69
msg.Subject = subject;
70
//指定邮件正文的编码
71
msg.Charset = "gb2312";
72
//发送邮件
73
smtp.SendMail(msg);
74
}
75
}
76
}
77
78
public class SmtpSetting
79
{
80
private string _server;
81
82
public string Server
83
{
84
get
{ return _server; }
85
set
{ _server = value; }
86
}
87
private bool _authentication;
88
89
public bool Authentication
90
{
91
get
{ return _authentication; }
92
set
{ _authentication = value; }
93
}
94
private string _user;
95
96
public string User
97
{
98
get
{ return _user; }
99
set
{ _user = value; }
100
}
101
private string _sender;
102
103
public string Sender
104
{
105
get
{ return _sender; }
106
set
{ _sender = value; }
107
}
108
private string _password;
109
110
public string Password
111
{
112
get
{ return _password; }
113
set
{ _password = value; }
114
}
115
}
116
117
public class SmtpConfig
118
{
119
private static SmtpConfig _smtpConfig;
120
private string ConfigFile
121
{
122
get
123
{
124
string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
125
if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
126
{
127
configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");
128
}
129
else
130
{
131
if (!Path.IsPathRooted(configPath))
132
configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
133
else
134
configPath = Path.Combine(configPath, "SmtpSetting.config");
135
}
136
return configPath;
137
}
138
}
139
public SmtpSetting SmtpSetting
140
{
141
get
142
{
143
XmlDocument doc = new XmlDocument();
144
doc.Load(this.ConfigFile);
145
SmtpSetting smtpSetting = new SmtpSetting();
146
smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
147
smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
148
smtpSetting.User = doc.DocumentElement.SelectSingleNode("User").InnerText;
149
smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
150
smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;
151
152
return smtpSetting;
153
}
154
}
155
private SmtpConfig()
156
{
157
158
}
159
public static SmtpConfig Create()
160
{
161
if (_smtpConfig == null)
162
{
163
_smtpConfig = new SmtpConfig();
164
}
165
return _smtpConfig;
166
}
167
}

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
