/// <summary>
2
/// 自定義方法用來發(fā)送郵件
3
/// </summary>
4
/// <param name="Sender">發(fā)件人地址</param>
5
/// <param name="Receiver">收件人地址</param>
6
/// <param name="Subject">郵件標題</param>
7
/// <param name="Content">郵件內(nèi)容</param>
8
/// <param name="server">服務(wù)器名</param>
9
/// <returns>返回一個布爾值,如果返回True表示發(fā)送成功!否則為false</returns>
10
11
#region sendMail1 Jmail
12
public bool sendMail1(string Sender, string Receiver,string Name,string Pwd, string Subject, string Content, string server)
13
{
14
//int sunEmail = 0;
15
jmail.MessageClass myJmail = new jmail.MessageClass();
16
myJmail.Charset = "GB2312"; //設(shè)置使用的郵件字符集,默認US-ASCII 中國則為GB2312
17
myJmail.Encoding = "base64"; //
18
myJmail.ISOEncodeHeaders = false; //郵件頭是否使用iso-8859-1編碼 默認值為true;
19
//myJmail.ContentType="text/html";
20
myJmail.Priority =Convert.ToByte(1); //優(yōu)先級別 1最高
21
myJmail.From = Sender; //返回或設(shè)置發(fā)件人的地址
22
myJmail.MailServerUserName = Name;//發(fā)送人郵箱用戶名
23
myJmail.MailServerPassWord = Pwd; //發(fā)送人郵箱密碼
24
25
//myJmail.AddHeader("Priority","3");
26
//myJmail.AddHeader("MSMail-Priority", "Normal");
27
//myJmail.AddHeader("Mailer","Microsoft Outlook Express 6.00.2800.1437");
28
//myJmail.AddHeader("MimeOLE","Produced By Microsoft MimeOLE V6.00.2800.1441");
29
30
myJmail.Subject = Subject; //郵件的主題(標題)
31
myJmail.AddRecipient(Receiver, "", ""); //添加收件人
32
if (FileUp.PostedFile.ContentLength != 0)
33
{
34
string filePath = FileUp.PostedFile.FileName;
35
myJmail.AddAttachment(@filePath,false,""); //添加一個附件
36
}
37
38
Content ="<html>"
39
+"<head>"
40
+"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />"
41
+"<title>SendEmail</title>"
42
+"<style type=\"text/css\">"
43
+"</style>"
44
+"</head>"
45
+"<body>"
46
+System.DateTime.Now
47
+"<hr/>"
48
+ Content
49
+"</body>"
50
+"</html>";
51
52
myJmail.Body = Content; //郵件的正文
53
54
return myJmail.Send(server, true);//發(fā)送郵件
55
56
}
57
#endregion sendMail1
58
59
#region sendMail2 SmtpClient
60
public bool sendMail2(string fromAddre, string toAddre, string subject, string body, string userName, string password, string smtpHost)
61
{
62
63
MailMessage message = new MailMessage();
64
65
message.From = new MailAddress(fromAddre, userName, System.Text.Encoding.GetEncoding("gb2312"));
66
message.To.Add(new MailAddress(toAddre, toAddre, System.Text.Encoding.GetEncoding("gb2312")));
67
message.Subject = subject;//設(shè)置郵件主題
68
message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
69
message.IsBodyHtml = true;//設(shè)置郵件正文為html格式
70
message.Body = body;//設(shè)置郵件內(nèi)容
71
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
72
message.Priority = MailPriority.High;
73
74
SmtpClient client = new SmtpClient(); //smtp服務(wù)器
75
76
client.Host = smtpHost;
77
//client.Port = 25;
78
//client.Credentials = new NetworkCredential(fromAddre, password); //用戶名憑證
79
client.DeliveryMethod = SmtpDeliveryMethod.Network; //設(shè)置發(fā)送方式
80
client.UseDefaultCredentials = true;
81
CredentialCache myCache = new CredentialCache();
82
myCache.Add(smtpHost,25,"login", new NetworkCredential(userName, password));
83
client.Credentials = myCache;
84
if (FileUp.PostedFile.ContentLength != 0)
85
{
86
string filePath = FileUp.PostedFile.FileName;
87
Attachment data = new Attachment(@filePath);
88
message.Attachments.Add(data); //添加一個附件
89
}
90
try
91
{
92
client.Send(message);
93
return true;
94
}
95
catch (Exception ex)
96
{
97
//Response.Write("<script>alert('" + ex.Message + "');</script>");
98
return false;
99
}
100
101
}
102
#endregion
103
104
#region sendmail3 SmtpClient
105
public bool sendMail3(string Sender, string Receiver, string Name, string Pwd, string Subject, string Content, string server)
106
{
107
108
System.Net.Mail.MailAddress senderAddresss = new MailAddress(Sender);
109
System.Net.Mail.MailAddress receiverAddresss = new MailAddress(Receiver);
110
System.Net.Mail.MailMessage message = new MailMessage(senderAddresss, receiverAddresss);
111
message.Subject = Subject;
112
message.Body = Content;
113
message.BodyEncoding = System.Text.Encoding.UTF8;
114
message.IsBodyHtml = true;
115
116
System.Net.Mail.SmtpClient client = new SmtpClient();
117
client.Host = server;
118
client.UseDefaultCredentials = true;
119
client.Credentials = new System.Net.NetworkCredential(Name, Pwd);
120
121
client.DeliveryMethod = SmtpDeliveryMethod.Network;
122
123
//添加附件
124
//Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
125
// message.Attachments.Add(data);
126
127
try
128
{
129
client.Send(message);
130
return true;
131
132
}
133
catch (Exception ex)
134
{
135
return false;
136
}
137
138
}
139
#endregion
140
141
#region sendMail4 web.mail
142
public bool sendmail4(string Sender, string Receiver, string Name, string Pwd, string Subject, string Content, string server)
143
{
144
MailMessage mailMsg = new MailMessage();
145
//發(fā)送地址
146
mailMsg.From = Sender;
147
//接收地址
148
mailMsg.To = Receiver;
149
//設(shè)置郵件正文內(nèi)容的類型式
150
mailMsg.BodyFormat = MailFormat.Text;
151
//郵件主題
152
mailMsg.Subject = Subject;
153
154
// 創(chuàng)建一個附件對象
155
//MailAttachment ma = new MailAttachment(f.Value);//f.value附件完整路徑
156
157
// mailMsg.Attachments.Add(ma);
158
159
160
//郵件內(nèi)容
161
mailMsg.Body =Content;
162
//服務(wù)器端的ip,因為我們用的是本地的虛擬smtp服務(wù)器,所以只需要填寫本地ip地址
163
//SmtpMail.SmtpServer = "127.0.0.1";
164
165
166
167
///以下三條一般都要加 一般的郵箱服務(wù)器都需要身份驗證
168
169
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
170
//設(shè)置驗證用戶名(把userName改為你的驗證用戶名)
171
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Name);
172
//設(shè)置驗證密碼(把pwd改為你的驗證密碼) //發(fā)件人用戶名
173
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Pwd);//郵箱密碼
174
175
176
SmtpMail.SmtpServer = server; //可選擇其他服務(wù)器
177
try
178
{
179
SmtpMail.Send(mailMsg);
180
return true;
181
}
182
catch (Exception ex)
183
{
184
Response.Write("<script>alert('"+ex.Message+"');</script>");
185
return false;
186
}
187
188
}
189
#endregion
190
191
#region sendMail5 OpenSmtp
192
193
public bool sendMail5(string Sender, string Receiver, string Name, string Pwd, string Subject, string Content, string server)
194
{
195
OpenSmtp.Mail.MailMessage OpMMsg = new OpenSmtp.Mail.MailMessage();
196
197
OpenSmtp.Mail.SmtpConfig.VerifyAddresses = false;
198
OpMMsg.Charset = "gb2312";
199
OpMMsg.Priority = OpenSmtp.Mail.MailPriority.High;
200
OpMMsg.From =new OpenSmtp.Mail.EmailAddress(Sender);
201
OpMMsg.AddRecipient(Receiver,OpenSmtp.Mail.AddressType.To);
202
OpMMsg.Subject = Subject;
203
OpMMsg.Body = Content;
204
205
206
OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp();
207
208
smtp.Host = server;
209
smtp.Username = Name;
210
smtp.Password = Pwd;
211
smtp.Port = 25;
212
try
213
{
214
smtp.SendMail(OpMMsg);
215
return true;
216
}
217
catch
218
{
219
return false;
220
}
221
}
222
223
#endregion
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

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223
