在項(xiàng)目開發(fā)中,發(fā)送郵件時(shí)一種非常常見的功能。一般的情況下,大型的公司都有自己的郵件系統(tǒng),我們可以直接通過公司的pop/smtp server進(jìn)行郵件的發(fā)送和接收。不過,對于一些小公司不具有這樣的條件,他們一般通過一些公共的郵件服務(wù)通過商提供的郵件服務(wù)。比如sina,163就是很好的、常用的郵件服務(wù)。不過相比之下,我還是習(xí)慣使用google gmail。
一、在.net中通過gmail發(fā)送郵件 
我們知道,smtp是我們最常用的郵件傳輸?shù)膮f(xié)議。通過smtp方式,我們只需要配置相應(yīng)的stmp server和port,使用我們的帳號和密碼登錄到stmp server,理論上我們就可以進(jìn)行郵件的發(fā)送了。對于google gmail,對應(yīng)的信息如下:
pop3 server (port: 995) :pop.gmail.com, ssl
smtp server (port: 25, 465, 587):smtp.gmail.com, tls 
你通過你注冊的gmail帳號和密碼就可以登錄smtp.gmail.com。下面是一段簡單的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 = "[email protected]";
        const string address_to = "[email protected]"; 
        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應(yīng)該是很熟悉了,在這里我就不想對上面的邏輯多做介紹了。不過我需要補(bǔ)充幾點(diǎn)的是:
1.通過gmail,你只能以你登錄到smtp server的account的名義對外發(fā)信,以上面為例,我以” myaccount”最為gmail的account登錄,向email address 為[email protected]發(fā)送郵件,雖然在smtpclient.send方法中的我指定的from address為[email protected],當(dāng)收信人受到該郵件的時(shí)候,郵件的發(fā)件人是[email protected],不會為[email protected]。這些很有必要的,可以防止你利用別人的名義發(fā)送郵件。這種機(jī)制并不是通用的,我就和同事開過這樣的玩笑:通過公司的stmp server以另一個(gè)同事的名義向他發(fā)郵件。
2.雖然google對外宣稱他們開發(fā)的smtp server的port為25,465和587,但是在代碼中,我使用25和587一切正常,當(dāng)時(shí)當(dāng)我使用465的時(shí)候,怎么也發(fā)不出去。但是當(dāng)我在outlook中把port配置為465的時(shí)候,發(fā)送郵件也正常。我還沒來得及查閱到底是什么問題。知道原因的朋友,請不吝賜教。
3.對于像這種郵件服務(wù)功能的代碼,我們一般寫成可配置的。因?yàn)閷τ趯τ趲艉兔艽a,甚至是stmp server,都有可能經(jīng)常的變換。但是我們不用通過常用的<appsettings>來配置,也不用定義我們的custom configurationsection。因?yàn)閏onfiguration system已經(jīng)為我們定義的內(nèi)置的<mailsettings>來配置郵件相關(guān)的信息。比如:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net> 
    <mailsettings>
      <smtp from="[email protected]">
        <network host="smtp.gmail.com"
                 password="password" 
                 port="587"
                 username=" myaccount @gmail.com"/>
      </smtp>
    </mailsettings> 
  </system.net>
</configuration>
對于gmail,from實(shí)際上沒有什么意義。
現(xiàn)在我們就可以進(jì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);
        }
新聞熱點(diǎn)
疑難解答
圖片精選