.net中使用Gmail发送邮件

本文介绍如何使用.NET通过Gmail的SMTP服务器发送邮件,并提供了一段C#代码示例。文章还讨论了一些注意事项,例如只能以登录账户名义发送邮件以及不同端口的使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目开发中,发送邮件时一种非常常见的功能。一般的情况下,大型的公司都有自己的邮件系统,我们可以直接通过公司的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      {
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>来配置邮件相关的信息。比如:

 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" />
10         </ smtp >
11       </ mailSettings >  
12     </ system.net >
13  </ configuration >

 

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

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


1  static   void  SendMail()
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;):

1          MailMessage mail  =  e.Message;
2          e.Cancel  =   true ;
3          SmtpClient smtp  =   new  SmtpClient();
4          smtp.EnableSsl  =   true ;        
5          smtp.Send(mail);

这样就搞定了。

然后我再查查怎么丰富一下邮件内容,即设置(Mail)BodyFile 

 

转载于:https://www.cnblogs.com/yuanqiang/archive/2008/10/22/1317036.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值