在項(xiàng)目開(kāi)發(fā)中,發(fā)送郵件時(shí)一種非常常見(jiàn)的功能。一般的情況下,大型的公司都有自己的郵件系統(tǒng),我們可以直接通過(guò)公司的Pop/SMTP Server進(jìn)行郵件的發(fā)送和接收。不過(guò),對(duì)于一些小公司不具有這樣的條件,他們一般通過(guò)一些公共的郵件服務(wù)通過(guò)商提供的郵件服務(wù)。比如Sina,163就是很好的、常用的郵件服務(wù)。不過(guò)相比之下,我還是習(xí)慣使用Google Gmail。
一、在.net中通過(guò)Gmail發(fā)送郵件
我們知道,SMTP是我們最常用的郵件傳輸?shù)膮f(xié)議。通過(guò)SMTP方式,我們只需要配置相應(yīng)的STMP Server和Port,使用我們的帳號(hào)和密碼登錄到STMP Server,理論上我們就可以進(jìn)行郵件的發(fā)送了。對(duì)于Google Gmail,對(duì)應(yīng)的信息如下:
Pop3 Server (Port: 995) :pop.gmail.com, SSL
SMTP Server (Port: 25, 465, 587):smtp.gmail.com, TLS
你通過(guò)你注冊(cè)的Gmail帳號(hào)和密碼就可以登錄smtp.gmail.com。下面是一段簡(jiǎn)單的C# Code。
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,對(duì)上面的Code應(yīng)該是很熟悉了,在這里我就不想對(duì)上面的邏輯多做介紹了。不過(guò)我需要補(bǔ)充幾點(diǎn)的是:
1.通過(guò)Gmail,你只能以你登錄到SMTP Server的Account的名義對(duì)外發(fā)信,以上面為例,我以” MyAccount”最為Gmail的Account登錄,向Email address 為to@gmail.com發(fā)送郵件,雖然在SmtpClient.Send方法中的我指定的From address為from@gail.com,當(dāng)收信人受到該郵件的時(shí)候,郵件的發(fā)件人是MyAccount@gail.com,不會(huì)為from@gail.com。這些很有必要的,可以防止你利用別人的名義發(fā)送郵件。這種機(jī)制并不是通用的,我就和同事開(kāi)過(guò)這樣的玩笑:通過(guò)公司的STMP Server以另一個(gè)同事的名義向他發(fā)郵件。
2.雖然Google對(duì)外宣稱他們開(kāi)發(fā)的SMTP Server的Port為25,465和587,但是在代碼中,我使用25和587一切正常,當(dāng)時(shí)當(dāng)我使用465的時(shí)候,怎么也發(fā)不出去。但是當(dāng)我在Outlook中把Port配置為465的時(shí)候,發(fā)送郵件也正常。我還沒(méi)來(lái)得及查閱到底是什么問(wèn)題。知道原因的朋友,請(qǐng)不吝賜教。
3.對(duì)于像這種郵件服務(wù)功能的代碼,我們一般寫(xiě)成可配置的。因?yàn)閷?duì)于對(duì)于帳戶和密碼,甚至是STMP Server,都有可能經(jīng)常的變換。但是我們不用通過(guò)常用的<AppSettings>來(lái)配置,也不用定義我們的Custom ConfigurationSection。因?yàn)镃onfiguration System已經(jīng)為我們定義的內(nèi)置的<mailSettings>來(lái)配置郵件相關(guān)的信息。比如:
<?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>
對(duì)于Gmail,from實(shí)際上沒(méi)有什么意義。
現(xiàn)在我們就可以進(jìn)一步地簡(jiǎn)化我們的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);
}
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答??! 點(diǎn)擊進(jìn)入論壇
帳號(hào)名字寫(xiě)錯(cuò),修改成羅興衛(wèi)