程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> Asp.net發送郵件的兩種方法小結

Asp.net發送郵件的兩種方法小結

編輯:ASP.NET基礎
一,利用大網站的SMTP來發送郵件

這種方法適用於程序運行環境沒有配置SMTP的服務器,想借助於其他smtp來發送郵件的情況,當然需要有此smtp的賬戶才行,例如如果使用Google的SMTP服務器,有三點需要注意:啟用SSL,端口和地址smtp.gmail.com。

二,利用本地的smtp來發送郵件

這種方法要求本地有smtp服務器,如果沒有,windows 7和vista上面沒有smtp服務器可以安裝一個軟件,

Free SMTP Server,下載地址:http://www.softstack.com/freesmtp.html,這種方式不用提供用戶名,只需要設置一下IIS即可。 

做如下設置:

相關代碼如下:
復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace IISSendMail
{
class Program
{
static void Main(string[] args)
{
/*第一種,利用Google的smtp來發送郵件*/
SmtpClient client =
new SmtpClient("smtp.gmail.com", 25);
MailMessage msg =
new MailMessage("[email protected]","[email protected]","這個是標題","這個是內容");
client.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo =
new System.Net.NetworkCredential("username", "password");
client.Credentials = basicAuthenticationInfo;
client.EnableSsl = true;
client.Send(msg);

/*第二種,利用本地的smtp來發送郵件*/
SmtpClient smtp =
new SmtpClient("localhost", 25);
MailMessage message =
new MailMessage("[email protected]", "[email protected]", "標題:測試一下iis發郵件", "內容:老翁,你好!哈哈");
smtp.Send(message);

Console.WriteLine("發送成功!");
Console.Read();
}
}
}

作者:cnblogs LeonWeng
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved