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)過我測試通過了的代碼,只需要將郵件的用戶名和密碼修改成自己的即可,同時(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對應(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;
}
//測試發(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)文章
C#通過實(shí)現(xiàn)winmm枚舉音頻設(shè)備
使用C#做音頻錄制時(shí)需要獲取音頻設(shè)備信息,其中比較簡單的就是使用winmm,所以本文就為大家介紹一下C#如何通過實(shí)現(xiàn)winmm枚舉音頻設(shè)備,需要的可以參考下2023-10-10
基于WPF實(shí)現(xiàn)ListBox拖動(dòng)子項(xiàng)
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)ListBox拖動(dòng)子項(xiàng)效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-04-04
C# 設(shè)計(jì)模式系列教程-狀態(tài)模式
狀態(tài)模式主要解決的是當(dāng)控制一個(gè)對象狀態(tài)轉(zhuǎn)換的條件表達(dá)式過于復(fù)雜時(shí)的情況。把狀態(tài)的判斷邏輯轉(zhuǎn)移到表示不同的一系列類當(dāng)中,可以把復(fù)雜的邏輯判斷簡單化。2016-06-06

