c#利用webmail郵件系統(tǒng)發(fā)送郵件示例分享
在C#中發(fā)送郵件的方式有2種,一種是使用webmail方式進(jìn)行發(fā)送,另外一種就是采用netmail發(fā)送的方式,在采用這2種方式發(fā)送郵件時(shí),如果采用公用的郵件服務(wù)器(如126郵件服務(wù)器,Sina的郵件服務(wù)器)都是需要授權(quán)認(rèn)證才能夠發(fā)送,如果是采用Gmail的話,還會(huì)有每天發(fā)送郵件的數(shù)量等限制。這2種方式是經(jīng)過我測(cè)試通過了的代碼,只需要將郵件的用戶名和密碼修改成自己的即可,同時(shí)也可以修改郵件服務(wù)器,改成自己配置的郵件服務(wù)器。
/// <summary>
/// 發(fā)送Email(帶驗(yàn)證,采用微軟新推薦的方式)
/// </summary>
/// <param name="strTo">收件Email</param>
/// <param name="strCc">抄送Email</param>
/// <param name="strSubject">標(biāo)題</param>
/// <param name="strBody">內(nèi)容</param>
/// <param name="UserName">郵箱驗(yàn)證帳號(hào)(與web.config里配置的帳號(hào)要一樣)</param>
/// <param name="from">發(fā)信人郵箱,要與UserName對(duì)應(yīng)</param>
/// <param name="strErrorMsg">錯(cuò)誤消息</param>
/// <returns></returns>
public static bool WebSendEmail(string strTo, string strCc, string strSubject, string strBody, ref string strErrorMsg)
{
System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
bool bState = false;
string strSMTPServer = "";
try
{
strSMTPServer = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SMTP"]);
strSMTPServer = strSMTPServer == "" ? "localhost" : strSMTPServer;
string strFromAddr = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["FromAddress"]);
if (reg.IsMatch(strFromAddr))
{
message.From = strFromAddr;
}
else
{
throw new Exception("The Email Address is wrong,Please reset the Email Address in the web.config file !");
}
string strTemp = "";
foreach (string str in strTo.Split(';'))
{
if (reg.IsMatch(str))
if (!strTemp.Contains(str))
strTemp += str + ";";
}
message.Cc = "";
foreach (string str in strCc.Split(';'))
{
if (reg.IsMatch(str))
if (!message.Cc.Contains(str))
message.Cc += str + ";";
}
message.Subject = strSubject;
message.BodyFormat = System.Web.Mail.MailFormat.Html;
message.Body ="<html><body>UtilMailMessage001"+ strBody+"- success</body></html>" ;
//下面這塊是加載附件的方法
MailAttachment attachment1 =new MailAttachment(@"d:\My Documents\test1.doc");
MailAttachment attachment2 =new MailAttachment("d:\\Documents\\test2.doc");
message.Attachments.Add(attachment1);
message.Attachments.Add(attachment2);
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//這里的郵箱帳號(hào)和密碼一定要和下面配置文件中設(shè)置的郵箱的帳號(hào)和密碼一致
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxxxxxxxx");//郵箱帳號(hào),比如Test11@126.com帳號(hào)為:Test11
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxxxxxxx");//郵箱密碼
//這個(gè)是指明郵件服務(wù)器的端口,可以不指定
//message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "25");
foreach (string str in strTemp.Split(';'))
{
if (reg.IsMatch(str))
{
message.To = str;
message.BodyEncoding = System.Text.Encoding.UTF8;
System.Web.Mail.SmtpMail.SmtpServer = strSMTPServer;
System.Web.Mail.SmtpMail.Send(message);
}
}
bState = true;
}
catch (Exception ex)
{
System.IO.File.AppendAllText("C:\\Mail_Log.ini", string.Format("{0:yyyy/MM/dd HH:mm:ss}\r\n{1}\r\n\r\n", DateTime.Now, ex.Message));
bState = false;
strErrorMsg = ex.Message;
}
return bState;
}
//測(cè)試發(fā)送郵件
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
Email.SendEmail("xxxxxx@163.com", "", "Test Email", "Test Send Email");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
郵件在webconfig文件中配置如下:
相關(guān)文章
深入多線程之:Reader與Write Locks(讀寫鎖)的使用詳解
本篇文章是對(duì)Reader與Write Locks(讀寫鎖)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#實(shí)現(xiàn)鬧鐘AlarmClock實(shí)例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)鬧鐘AlarmClock實(shí)例代碼,很實(shí)用的功能,需要的朋友可以參考下2014-08-08

解析c#操作excel后關(guān)閉excel.exe的方法

C#中遍歷DataSet數(shù)據(jù)集對(duì)象實(shí)例

C#多線程開發(fā)實(shí)戰(zhàn)記錄之線程基礎(chǔ)