最近一年,一直在和一個澳大利亞的客戶做金融相關的項目,由於客戶那邊沒有專門的IT相關的開發和維護人員,所有的溝通都是基於Email和skype的方式,郵件系統是他們平時最為依賴的一部分,只要這部分出問題了,再多的其他正在進行的事情都要擱置起來,先解決這個,所以將這個放在第一部分:
Step1:在IIS7.5下啟用IIS6的SMTP,見下圖:

Step2:點擊SMTP-->Propertites:
Step3:設置郵件IP地址:
Step4:這一步比較重要,設置Relay restriction開始就是沒設置這一步導致浪費了一天時間:
在彈出的頁面選擇All except the list below:
Step5:設置允許的最大郵件大小,每天最大連接數:

Step6: 設置郵件發送失敗重發的郵件頻率等等:
OK,這樣在相關的設置就完成了。
相關的C#的SMTP操作的代碼如下:
1) 發送不帶附件的郵件
1 /// <summary>
2
///
Send email without attachments
3 /// </summary>
4 /// <param name="ToMail"> 收件人郵箱地址 </param>
5 /// <param name="FromMail"> 發件人郵箱地址 </param>
6 /// <param name="Cc"> 抄送 </param>
7 /// <param name="Bcc"> 密送 </param>
8 /// <param name="Body"> 郵件正文 </param>
9 /// <param name="Subject"> 郵件標題 </param>
10 /// <returns></returns>
11 public string SendMail( string ToMail, string FromMail, string Cc, string Bcc, string Body, string Subject)
12 {
13 SmtpClient client = new SmtpClient();
14 MailMessage message = new MailMessage {
15 From = new MailAddress(FromMail)
16 };
17 message.To.Add(ToMail);
18 if (Cc != "")
19 {
20 message.CC.Add(Cc);
21 }
22 message.Body = Body;
23 message.Subject = Subject;
24 message.IsBodyHtml = true;
25 client.UseDefaultCredentials = true;
26 message.Priority = MailPriority.High;
27 client.Host = " 127.0.0.1 "; // 此處應該改為上面設置的服務器IP地址
28 client.Port = 0x19;
29 try
30 {
31 client.DeliveryMethod = SmtpDeliveryMethod.Network;
32 client.Send(message);
33 message.Dispose();
34 return " 1 ";
35 }
36 catch (Exception exception)
37 {
38 return ( " 0 " + exception);
39 }
40 }
3 /// </summary>
4 /// <param name="ToMail"> 收件人郵箱地址 </param>
5 /// <param name="FromMail"> 發件人郵箱地址 </param>
6 /// <param name="Cc"> 抄送 </param>
7 /// <param name="Bcc"> 密送 </param>
8 /// <param name="Body"> 郵件正文 </param>
9 /// <param name="Subject"> 郵件標題 </param>
10 /// <returns></returns>
11 public string SendMail( string ToMail, string FromMail, string Cc, string Bcc, string Body, string Subject)
12 {
13 SmtpClient client = new SmtpClient();
14 MailMessage message = new MailMessage {
15 From = new MailAddress(FromMail)
16 };
17 message.To.Add(ToMail);
18 if (Cc != "")
19 {
20 message.CC.Add(Cc);
21 }
22 message.Body = Body;
23 message.Subject = Subject;
24 message.IsBodyHtml = true;
25 client.UseDefaultCredentials = true;
26 message.Priority = MailPriority.High;
27 client.Host = " 127.0.0.1 "; // 此處應該改為上面設置的服務器IP地址
28 client.Port = 0x19;
29 try
30 {
31 client.DeliveryMethod = SmtpDeliveryMethod.Network;
32 client.Send(message);
33 message.Dispose();
34 return " 1 ";
35 }
36 catch (Exception exception)
37 {
38 return ( " 0 " + exception);
39 }
40 }
2)發送帶附件的郵件
2
///
Send email without attachments
3 /// </summary>
4 /// <param name="ToMail"> 收件人郵箱地址 </param>
5 /// <param name="FromMail"> 發件人郵箱地址 </param>
6 /// <param name="Cc"> 抄送 </param>
7 /// <param name="Bcc"> 密送 </param>
8 /// <param name="Body"> 郵件正文 </param>
9 /// <param name="Subject"> 郵件標題 </param>
10 /// <param name="Attachments"> 附件列表 </param>
11 /// <returns></returns>
12 public string SendMailWithAttachment( string ToMail, string FromMail, string Cc, string Bcc, string Body, string Subject, string[] Attachments)
13 {
14 SmtpClient client = new SmtpClient();
15 MailMessage message = new MailMessage {
16 From = new MailAddress(FromMail)
17 };
18 message.To.Add(ToMail);
19 if (Cc != "")
20 {
21 message.CC.Add(Cc);
22 }
23
24 message.Body = Body;
25 message.Subject = Subject;
26 message.IsBodyHtml = true;
27 message.Priority = MailPriority.High;
28 if (Attachments.Length > 0)
29 {
30 for ( int i = 0; i < Attachments.Length; i++)
31 {
32 if (Attachments[i].ToString() != "")
33 {
34 Attachment item = new Attachment(Attachments[i].ToString());
35 message.Attachments.Add(item);
36 }
37 }
38 }
39 client.Host = " 127.0.0.1 "; // 此處應該改為上面設置的服務器IP地址
40 client.Port = 0x19;
41 try
42 {
43 client.DeliveryMethod = SmtpDeliveryMethod.Network;
44 client.Send(message);
45 message.Dispose();
46 return " 1 ";
47 }
48 catch (Exception exception)
49 {
50 return ( " 0 " + exception);
51 }
52 }
3 /// </summary>
4 /// <param name="ToMail"> 收件人郵箱地址 </param>
5 /// <param name="FromMail"> 發件人郵箱地址 </param>
6 /// <param name="Cc"> 抄送 </param>
7 /// <param name="Bcc"> 密送 </param>
8 /// <param name="Body"> 郵件正文 </param>
9 /// <param name="Subject"> 郵件標題 </param>
10 /// <param name="Attachments"> 附件列表 </param>
11 /// <returns></returns>
12 public string SendMailWithAttachment( string ToMail, string FromMail, string Cc, string Bcc, string Body, string Subject, string[] Attachments)
13 {
14 SmtpClient client = new SmtpClient();
15 MailMessage message = new MailMessage {
16 From = new MailAddress(FromMail)
17 };
18 message.To.Add(ToMail);
19 if (Cc != "")
20 {
21 message.CC.Add(Cc);
22 }
23
24 message.Body = Body;
25 message.Subject = Subject;
26 message.IsBodyHtml = true;
27 message.Priority = MailPriority.High;
28 if (Attachments.Length > 0)
29 {
30 for ( int i = 0; i < Attachments.Length; i++)
31 {
32 if (Attachments[i].ToString() != "")
33 {
34 Attachment item = new Attachment(Attachments[i].ToString());
35 message.Attachments.Add(item);
36 }
37 }
38 }
39 client.Host = " 127.0.0.1 "; // 此處應該改為上面設置的服務器IP地址
40 client.Port = 0x19;
41 try
42 {
43 client.DeliveryMethod = SmtpDeliveryMethod.Network;
44 client.Send(message);
45 message.Dispose();
46 return " 1 ";
47 }
48 catch (Exception exception)
49 {
50 return ( " 0 " + exception);
51 }
52 }
