gmail的smtp采用了ssl连接:
Outgoing Mail (SMTP) Server - requires TLS: smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587
知道了gmail的发信细节,用System.Net.Mail,就是下面这段代码就可以了
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
![](/Images/OutliningIndicators/None.gif)
namespace GMailSend
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Program prg = new Program();
prg.Host = "smtp.gmail.com";
prg.SmtpUsername = "zhangshanyou@gmail.com";
prg.SmtpPassword = "xxxxxxxx";
prg.Port = 587;
prg.Send("zhangshanyou@gmail.com","33235911@qq.com", "test", "test", null, null);
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(ex.Message);
![](/Images/OutliningIndicators/InBlock.gif)
}
Console.Read();
}
![](/Images/OutliningIndicators/InBlock.gif)
private string _host;
![](/Images/OutliningIndicators/InBlock.gif)
public string Host
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _host; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _host = value; }
}
private int _port;
![](/Images/OutliningIndicators/InBlock.gif)
public int Port
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _port; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _port = value; }
}
private string _smtpUsername;
![](/Images/OutliningIndicators/InBlock.gif)
public string SmtpUsername
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _smtpUsername; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _smtpUsername = value; }
}
private string _smtpPassword;
![](/Images/OutliningIndicators/InBlock.gif)
public string SmtpPassword
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _smtpPassword; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _smtpPassword = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Send(string from, string to, string subject, string body, string[] cc, string[] bcc)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Create mail message
MailMessage message = new MailMessage(from, to, subject, body);
message.BodyEncoding = Encoding.GetEncoding(936);
![](/Images/OutliningIndicators/InBlock.gif)
if (cc != null && cc.Length > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (string ccAddress in cc)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
message.CC.Add(new MailAddress(ccAddress));
}
}
if (bcc != null && bcc.Length > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (string bccAddress in bcc)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
message.Bcc.Add(new MailAddress(bccAddress));
}
}
![](/Images/OutliningIndicators/InBlock.gif)
// Send email
SmtpClient client = new SmtpClient(this._host, this._port);
if (!String.IsNullOrEmpty(this._smtpUsername) && !String.IsNullOrEmpty(this._smtpPassword))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
client.Credentials = new NetworkCredential(this._smtpUsername, this._smtpPassword);
}
client.EnableSsl = true;
![](/Images/OutliningIndicators/InBlock.gif)
client.Send(message);
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/None.gif)