因為項目需要,我本來做的是通過密碼問題找回使用者忘記的密碼,結果客戶要郵件自動發送的那種找回密碼,還要用自己的郵件伺服器。那麼我怎麼辦?還要留個郵件伺服器介面,好難做啊,讓我真不知所措,我上網上查了一下午,也測試了一下午,郵件自動發送怎麼做,我用了差不多4種方法了,試了又試,我是在Webconfig裡配置,或是用比較流行的Jmail 。不知這算不算介面了,哎,郵件伺服器配置我不會配,也沒配過反正我就這樣做了,呵呵,如果有做過郵件伺服器配置的朋友看到我這個文章,希望能給我點提示,指點下我至於怎麼換成自己的郵件伺服器,又怎麼配置,就請高手指點下我吧。
下面說下我總結辦法:
第一種方法:
using System.Web.Mail;
public void sendMail()
{
MailMessage mail1 = new MailMessage();
mail1.Body="body here";
mail1.From="xxx@xxx.com";
mail1.To="yyy@yyy.com";
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1);
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","xxx@xxx.com");
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********");
SmtpMail.SmtpServer="mail.xxx.com";
SmtpMail.Send(mail1);
}
以上添加的幾個 Fields 是用來作SMTP發信認證的,如果你的發信伺服器不需要認證,就可以省略這幾句。
第二種方法:
using System.Net.Mail;
方法一:向單個地址發送郵件,不設定web.config檔案
public void SendMail()
{
string mailto = "to@company.com";
string mailfrom = "from@company.com";
System.Net.NetworkCredential credential = new System.Net.NetworkCredential("from_username", "from_password");
SmtpClient smtp = new SmtpClient("smtp.company.com");
smtp.Credentials = credential;
MailMessage message = new MailMessage(mailfrom, mailto);
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "subject here";
message.Body = "body here";
smtp.Send(message);
message.Dispose();
}
方法二、向單個地址發送郵件,設定web.config檔案
public void SendMail()
{
string mailto = "to@company.com";
string mailfrom = "from@company.com";
MailMessage message = new MailMessage(mailfrom, mailto);
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "subject here";
message.Body = "body here";
smtp.Send(message);
message.Dispose();
}
在web.config中添加如下:
<system.net>
<mailSettings>
<smtp from="from@company.com">
<network host="smtp.company.com" port="25" userName="from_username" password="from_password"/>
</smtp>
</mailSettings>
</system.net>
方法三:群發郵件,設定web.config檔案
public void SendEmail()
{
string mailto = "to1@company.com,to2@company.com";
string title = "mail title here";
string content = "mail content here";
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
MailAddressCollection address = new MailAddressCollection();
string[] mailtos = mailto.Split(',');
for (int i = 0; i < mailtos.Length; i++)
{
address.Add(mailtos[i]);
}
foreach (MailAddress add in address)
{
message.To.Add(add);
}
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = title;
message.Body = content;
smtp.Send(message);
message.Dispose();
address.Clear();
}
在web.config中添加如下:
<system.net>
<mailSettings>
<smtp from="from@company.com">
<network host="smtp.company.com" port="25" userName="from_username" password="from_password"/>
</smtp>
</mailSettings>
</system.net>
採用以上方法,如果運行發信程式的電腦上裝有郵件監控等殺毒軟體,會有失敗的警告,但實際已發送成功。解決辦法是關閉殺毒軟體的監控功能。
下面這個記得加上命名空間:
using System.Net;
using System.Net.Cache;
using System.Net.Mail;
using System.Net.Configuration;
using System.Web.Configuration;
適用的架構:asp.net framework 2.0/.net framework3.0/.net framework3.5
在web.config裡設定.net framework的網路連接
XML/HTML代碼
- <system.net>
- </system.net>
如下:設定stmp郵件發送的配置
XML/HTML代碼
- //父元素:configuration(提供所有名稱空間的配置)
- <system.net>
- //這裡還可以加入如下元素的設定
- //authenticationModules:設定用來驗證WEB請求的模組
- //connectionManagement:設定WEB伺服器最大串連數
- //defaultProxy:設定http的Proxy 伺服器
- //mailSettings:配置smtp
- //requestCaching:控制網路請求緩衝機制
- //settings:為System.NET配置基本網路選項
- //<webRequestModules>元素(網路設定):指定模組從WEB伺服器請求資訊。
- <mailSettings>
- //deliveryMethod設定郵件發送方式,這裡是網路形式
- <smtp deliveryMethod="Network" from="xxx@yyy.com" >
- //host郵件發送伺服器
- //userName發送郵件時,用來進行身分識別驗證的使用者名稱
- //password如下驗證時的密碼
- <network host="smtp.yyy.com" userName="xxx@yyy.com" password="zzzzzzz" />
- </smtp>
- </mailSettings>
- </system.net>
以編程的方式擷取web.config裡的smtp配置
NetSectionGroup類
命名空間
System.Net.Configuration:為應用程式提供了以編程方式訪問和更新設定檔System.Net命名空間下的設定的類。
程式集
System
定義:
public sealed class NetSectionGroup : ConfigurationSectionGroup
說明:
這個類提供了以程式方式訪問儲存在設定檔裡的資訊。
這個類和網路設定文檔裡的system.net元素(網路設定)相對應。
這部分的為下列名稱空間提供配置設定:
System.Net
System.Net.Cache
System.Net.Mail
在程式裡讀取configuration/system.net/mailSettings/stmp配置
C#代碼
- //NetSectionGroup在上面已經介紹過了
- //GetSectionGroup從指定的設定檔擷取system.net的配置
- //WebConfigurationManager.OpenWebConfiguration
- //開啟應用程式根目錄下的web.config產生System.Configuration.Configuration對象執行個體
- SmtpSection cfg = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/web.config")).MailSettings.Smtp;
寄送電子郵件
C#代碼
- try{
- //執行個體化一個郵件訊息對象
- MailMessage email = new MailMessage(cfg.From, mailto);
- email.IsBodyHtml = true;
- email.Body = "要發送的郵件內容,上面已經設定可以支援html內容,例外還可以通過指定email.BodyEncoding屬性設定郵件內容的編碼";
- email.Subject = "郵件主題,通過指定email.SubjectEncoding屬性設定郵件主題的編碼";
- //執行個體化smtp客服端對象,用來寄送電子郵件
- System.Net.Mail.SmtpClient stmp = new SmtpClient(cfg.Network.Host);
- //設定是否需要發送是否需要身分識別驗證,如果不需要下面的credentials是不需要的
- stmp.UseDefaultCredentials = true;
- stmp.Credentials = new System.Net.NetworkCredential(cfg.Network.UserName, cfg.Network.Password);
- //發送郵件
- stmp.Send(email);
- }
- catch (Exception ex)
- {
[轉]:http://www.cnblogs.com/matrix/archive/2004/05/20/10495.aspx
現在的郵件發送大多數需要STMP的身分識別驗證,
.NET裡面的
System.Web.Util
System.Web.Mail
就不可以了.
我寫這篇文章是希望對大家開發項目有所協助,高手見笑了
先要去找一個組件,名字叫 JMail
大家可以去網上找一下,下載下來安裝上.
將jmail.dll引用到工程中
private void Button1_Click(object sender, System.EventArgs e)
{
jmail.Message Jmail=new jmail.Message();
DateTime t=DateTime.Now;
String Subject=" From EMail .net";
String body="你好科學12:15";
String FromEmail="ljt21@163.com";
String ToEmail="xiao-maolover@163.com";
//Silent屬性:如果設定為true,JMail不會拋出例外錯誤. JMail. Send( () 會根據操作結果返回true或false
Jmail.Silent=true;
//Jmail建立的日誌,前提loging屬性設定為true
Jmail.Logging=true;
//字元集,預設為"US-ASCII"
Jmail.Charset="GB2312";
//信件的contentype. 預設是"text/plain") : 字串如果你以HTML格式發送郵件, 改為"text/html"即可。
Jmail.ContentType="text/html";
//添加收件者
Jmail.AddRecipient(ToEmail,"","");
Jmail.From=FromEmail;
//寄件者郵件使用者名
Jmail.MailServerUserName="ljt21" ;
//寄件者郵件密碼
Jmail.MailServerPassWord="****" ;
//設定郵件標題
Jmail.Subject=Subject;
//郵件添加附件,(多附件的話,可以再加一條Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,講把上面的Jmail.ContentType="text/html";刪掉。否則會在郵件裡出現亂碼。
Jmail.AddAttachment( "c:\\test.jpg",true,null);
//郵件內容
Jmail.Body=body+t.ToString();
//Jmail發送的方法
Jmail.Send("smtp.163.com",false);
Jmail.Close() ;
}
這樣就OK了..!
經過測試的,沒有問題.
Asp.net 自動發送郵件的方法
今天有一個模組需要自動發送郵件的功能,就隨便寫了一個,記錄一下作為積累。
一、首先需要配置web.config檔案:
<system.net>
<mailSettings>
<smtp from="Emailname">
<network host="smtp.163.com" userName="Emailname" password="Emailpassword"
port="25" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
二、然後編寫發送郵件的函數:
//// <summary>
/// 郵件發送方法(帶附件)
/// </summary>
/// <param name="mailto">收件者地址。如:receiver@163.com</param>
/// <param name="mailsubject">郵件標題</param>
/// <param name="mailbody">郵件內文</param>
/// <param name="mailFrom">郵件發送人地址。如:sender@163.com</param>
/// <param name="list">附件路徑</param>
/// <returns></returns>
public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
{
try
{
//郵件發送人地址
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom);
//如test@163.com,初步測試,用test@sina.com不行,用163的郵件伺服器,就必須用163郵箱的使用者名稱
//收件者地址
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如test@tom.com
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
mail.Subject = mailsubject;
mail.Body = mailbody;
//以下設定伺服器
System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient();
//以下為增加附件
int count = list.Count;
for (int i = 0; i < count; i++)
{
System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(list[i].ToString());
mail.Attachments.Add(data);
}
mySmth.Send(mail);
mail.Dispose();
return true;
}
catch
{
return false;
}
}
三、最後就是對函數的調用了:
//自動發送郵件
string mailSubject = "會員註冊確認函";
string mailBody = "本文內容。";
string mailFrom = ConfigurationManager.AppSettings["SendMail"];
ArrayList List = new ArrayList();
List.Add(Server.MapPath(ConfigurationManager.AppSettings["SendMailText"]));
if (MySendMail(this.txtEmail.Text, mailSubject, mailBody, mailFrom, List))
{
...
//發送成功,進行相應處理
}
else
{
...
//發送失敗,進行相應處理
return;
}