在项目开发中,发送邮件时一种非常常见的功能。一般的情况下,大型的公司都有自己的邮件系统,我们可以直接通过公司的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。
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 {
10 const string ADDRESS_FROM = " from@gail.com " ;
11 const string ADDRESS_TO = " to@gmail.com " ;
12 const string USER_ID = " MyAccount " ;
13 const string PASSWORD = " password " ;
14 const string SMTP_SERVER = " smtp.gmail.com " ;
15 const int PORT = 587 ;
16
17 static void Main( string [] args)
18 {
19
20 SendMail(SMTP_SERVER, PORT);
21 Console.Read();
22
23 }
24 static void SendMail( string smtpServer, int port)
25
26 {
27 SmtpClient mailClient = new SmtpClient(smtpServer, 587 );
28 mailClient.EnableSsl = true ;
29 NetworkCredential crendetial = new NetworkCredential(USER_ID, PASSWORD);
30 mailClient.Credentials = crendetial;
31 MailMessage message = new MailMessage(ADDRESS_FROM, ADDRESS_TO, " This is a subject " , " This is the body of the mail " );
32
33 mailClient.Send(message);
34 Console.WriteLine( " Mail has been sent to '{0}' " , ADDRESS_TO);
35 }
36 }
37 }
熟悉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>来配置邮件相关的信息。比如:
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" />
10 </ smtp >
11 </ mailSettings >
12 </ system.net >
13 </ configuration >
对于Gmail,from实际上没有什么意义。
现在我们就可以进一步地简化我们的Managed code了:
2 {
3 SmtpClient mailClient = new SmtpClient();
4 mailClient.EnableSsl = true ;
5 MailMessage message = new MailMessage(ADDRESS_FROM, ADDRESS_TO, " This is a subject " , " This is the body of the mail " );
6 mailClient.Send(message);
7 Console.WriteLine( " Mail has been sent to '{0}' " , ADDRESS_TO);
8 }
可见端口设定变为最重要的一项,看准了用2.0时不能用465端口,而要用587,昨晚上发后一半用了半个小时也不成功。
可是用passwordrecovery控件的时候出了问题,它默认的enablessl是false,我又懒得自己写一个出来,所以在该空间的sendingmail事件中添加如下代码(不要忘了using System.Net.Mail;):
2 e.Cancel = true ;
3 SmtpClient smtp = new SmtpClient();
4 smtp.EnableSsl = true ;
5 smtp.Send(mail);
这样就搞定了。
然后我再查查怎么丰富一下邮件内容,即设置(Mail)BodyFile