muugua 发表于 2015-11-3 10:10:23

Windows Server 2008 R2 相关配置(一):邮件服务器(转)

Windows Server 2008 R2 相关配置(一):邮件服务器
Posted on 2012-03-24 17:15 lucio 阅读(228) 评论(0) 编辑 收藏   最近一年,一直在和一个澳大利亚的客户做金融相关的项目,由于客户那边没有专门的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=&quot;ToMail&quot;>收件人邮箱地址</param>
5       /// <param name=&quot;FromMail&quot;>发件人邮箱地址</param>
6       /// <param name=&quot;Cc&quot;>抄送</param>
7       /// <param name=&quot;Bcc&quot;>密送</param>
8       /// <param name=&quot;Body&quot;>邮件正文</param>
9       /// <param name=&quot;Subject&quot;>邮件标题</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 != &quot;&quot;)
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 = &quot;127.0.0.1&quot;;//此处应该改为上面设置的服务器IP地址
28       client.Port = 0x19;
29         try
30         {
31             client.DeliveryMethod = SmtpDeliveryMethod.Network;
32             client.Send(message);
33             message.Dispose();
34             return &quot;1&quot;;
35         }
36         catch (Exception exception)
37         {
38             return (&quot;0&quot; &#43; exception);
39         }
40   }  2)发送带附件的邮件
2     ///Send email without attachments
3     /// </summary>
4     /// <param name=&quot;ToMail&quot;>收件人邮箱地址</param>
5     /// <param name=&quot;FromMail&quot;>发件人邮箱地址</param>
6     /// <param name=&quot;Cc&quot;>抄送</param>
7     /// <param name=&quot;Bcc&quot;>密送</param>
8     /// <param name=&quot;Body&quot;>邮件正文</param>
9     /// <param name=&quot;Subject&quot;>邮件标题</param>
10     /// <param name=&quot;Attachments&quot;>附件列表</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 != &quot;&quot;)
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&#43;&#43;)
31             {
32               if (Attachments.ToString() != &quot;&quot;)
33               {
34                     Attachment item = new Attachment(Attachments.ToString());
35                     message.Attachments.Add(item);
36               }
37             }
38         }
39         client.Host = &quot;127.0.0.1&quot;;//此处应该改为上面设置的服务器IP地址
40       client.Port = 0x19;
41         try
42         {
43             client.DeliveryMethod = SmtpDeliveryMethod.Network;
44             client.Send(message);
45             message.Dispose();
46             return &quot;1&quot;;
47         }
48         catch (Exception exception)
49         {
50             return (&quot;0&quot; &#43; exception);
51         }
52   }
页: [1]
查看完整版本: Windows Server 2008 R2 相关配置(一):邮件服务器(转)