您的位置:首页 > 大数据 > 人工智能

.net中使用Gmail发送邮件

2008-10-22 17:22 423 查看
在项目开发中,发送邮件时一种非常常见的功能。一般的情况下,大型的公司都有自己的邮件系统,我们可以直接通过公司的Pop/SMTP Server进行邮件的发送和接收。不过,对于一些小公司不具有这样的条件,他们一般通过一些公共的邮件服务通过商提供的邮件服务。比如Sina,163就是很好的、常用的邮件服务。不过相比之下,我还是习惯使用Google Gmail。

一、在.net中通过Gmail发送邮件

我们知道,SMTP是我们最常用的邮件传输的协议。通过SMTP方式,我们只需要配置相应的STMP Server和Port,使用我们的帐号和密码登录到STMP Server,理论上我们就可以进行邮件的发送了。对于Google Gmail,对应的信息如下:

Pop3 Server (Port: 995) :pop.gmail.com, SSL

SMTP Server (Port: 25, 465, 587):smtp.gmail.com, TLS

你通过你注册的Gmail帐号和密码就可以登录smtp.gmail.com。下面是一段简单的C# Code。

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Text;

 4 using System.Net.Mail;

 5 using System.Net;

 6 namespace Artech.Mail.ConsoleApp

 7 { 

 8     class Program

 9     {

         const string ADDRESS_FROM = "from@gail.com";

         const string ADDRESS_TO = "to@gmail.com"; 

         const string USER_ID = "MyAccount";

         const string PASSWORD = "password"; 

         const string SMTP_SERVER = "smtp.gmail.com";

         const int PORT = 587;

  

         static void Main(string[] args)

         {

 

                 SendMail(SMTP_SERVER, PORT);

                 Console.Read();        

            

         }

         static void SendMail(string smtpServer, int port)

 

         { 

             SmtpClient mailClient = new SmtpClient(smtpServer, 587);

             mailClient.EnableSsl = true;

             NetworkCredential crendetial = new NetworkCredential(USER_ID, PASSWORD);

             mailClient.Credentials = crendetial;

             MailMessage message = new MailMessage(ADDRESS_FROM, ADDRESS_TO, "This is a subject", "This is the body of the mail"); 

            

             mailClient.Send(message);

             Console.WriteLine("Mail has been sent to '{0}'", ADDRESS_TO);

         }

     }

 }

熟悉System.Net.Mail. SmtpClient,对上面的Code应该是很熟悉了,在这里我就不想对上面的逻辑多做介绍了。不过我需要补充几点的是:

1.通过Gmail,你只能以你登录到SMTP Server的Account的名义对外发信,以上面为例,我以” MyAccount”最为Gmail的Account登录,向Email address 为to@gmail.com发送邮件,虽然在SmtpClient.Send方法中的我指定的From address为from@gail.com,当收信人受到该邮件的时候,邮件的发件人是MyAccount@gail.com,不会为from@gail.com。这些很有必要的,可以防止你利用别人的名义发送邮件。这种机制并不是通用的,我就和同事开过这样的玩笑:通过公司的STMP Server以另一个同事的名义向他发邮件。

2.虽然Google对外宣称他们开发的SMTP Server的Port为25,465和587,但是在代码中,我使用25和587一切正常,当时当我使用465的时候,怎么也发不出去。但是当我在Outlook中把Port配置为465的时候,发送邮件也正常。我还没来得及查阅到底是什么问题。知道原因的朋友,请不吝赐教。

3.对于像这种邮件服务功能的代码,我们一般写成可配置的。因为对于对于帐户和密码,甚至是STMP Server,都有可能经常的变换。但是我们不用通过常用的<AppSettings>来配置,也不用定义我们的Custom ConfigurationSection。因为Configuration System已经为我们定义的内置的<mailSettings>来配置邮件相关的信息。比如:

 1 <?xml version="1.0" encoding="utf-8" ?>

 2 <configuration>

 3   <system.net> 

 4     <mailSettings>

 5       <smtp from="MyAccount@gmail.com">

 6         <network host="smtp.gmail.com"

 7                  password="password" 

 8                  port="587"

 9                  userName=" MyAccount @gmail.com"/>

       </smtp>

     </mailSettings> 

   </system.net>

 </configuration>

对于Gmail,from实际上没有什么意义。

现在我们就可以进一步地简化我们的Managed code了:

 static void SendMail()

         {

             SmtpClient mailClient = new SmtpClient();

             mailClient.EnableSsl = true;

             MailMessage message = new MailMessage(ADDRESS_FROM, ADDRESS_TO, "This is a subject", "This is the body of the mail");

             mailClient.Send(message);

             Console.WriteLine("Mail has been sent to '{0}'", ADDRESS_TO);

8         }

可见端口设定变为最重要的一项,看准了用2.0时不能用465端口,而要用587,昨晚上发后一半用了半个小时也不成功。

可是用passwordrecovery控件的时候出了问题,它默认的enablessl是false,我又懒得自己写一个出来,所以在该空间的sendingmail事件中添加如下代码(不要忘了using System.Net.Mail;):

         MailMessage mail = e.Message;

         e.Cancel = true;

         SmtpClient smtp = new SmtpClient();

         smtp.EnableSsl = true;        

         smtp.Send(mail);

这样就搞定了。

然后我再查查怎么丰富一下邮件内容,即设置(Mail)BodyFile 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: