using System.Web.Mail;

    // Mail initialization
MailMessage mailMsg = new MailMessage();
mailMsg.From = "test@test.com";
mailMsg.To = "desti@126.com";
//mailMsg.Cc = cc;
//mailMsg.Bcc = bcc;
mailMsg.Subject = "send mail use gmail";
mailMsg.BodyFormat = MailFormat.Text;
mailMsg.Body = "Hello here comes the sun";
mailMsg.Priority = MailPriority.High;
// Smtp configuration
SmtpMail.SmtpServer = "smtp.gmail.com";
// – smtp.gmail.com use smtp authentication
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "yourgmailaddress");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "youpassword");
// – smtp.gmail.com use port 465 or 587
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
// – smtp.gmail.com use STARTTLS (some call this SSL)
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
// try to send Mail
try
{
     SmtpMail.Send(mailMsg);
     Response.Write("send success");
}
catch (Exception ex)
{
     Response.Write(ex.Message);
}

转自:http://www.cnblogs.com/sunbingzibo/archive/2008/03/07/1094831.html

找dotnet的资料还是上csdn和cnblogs,在这里看到算你幸运,放心吧,2008年3月10日20点54分发送到126邮箱成功。

注意:G官方限制一天内同一封邮件最多发送到500个联系人。

以上代码符合.net1.1规范,2.0的代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace Artech.Mail.ConsoleApp
{
    
class Program
    
{

        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>来配置邮件相关的信息。比如:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.net>
    
<mailSettings>
      
<smtp from="MyAccount@gmail.com">
        
<network host="smtp.gmail.com"
                  password
="password"
                  port
="587"
                  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);
         }

http://www.2ey.cn/showart.asp?art_id=338

可见端口设定变为最重要的一项,看准了用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