天天看點

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

Windows Server 2008 R2 相關配置(一):郵件伺服器

Posted on 2012-03-24 17:15 lucio 閱讀(228) 評論( 0) 編輯 收藏

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

        最近一年,一直在和一個澳洲的客戶做金融相關的項目,由于客戶那邊沒有專門的IT相關的開發和維護人員,所有的溝通都是基于Email和skype的方式,郵件系統是他們平時最為依賴的一部分,隻要這部分出問題了,再多的其他正在進行的事情都要擱置起來,先解決這個,是以将這個放在第一部分:

   Step1:在IIS7.5下啟用IIS6的SMTP,見下圖:

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

Step2:點選SMTP-->Propertites:

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

Step3:設定郵件IP位址:

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

  Step4:這一步比較重要,設定Relay restriction開始就是沒設定這一步導緻浪費了一天時間:

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

        在彈出的頁面選擇All except the list below:

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

Step5:設定允許的最大郵件大小,每天最大連接配接數:

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

Step6: 設定郵件發送失敗重發的郵件頻率等等:

Windows Server 2008 R2 相關配置(一):郵件伺服器(轉)

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     }

 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     }