標籤:
在ASP.NET中實現發送郵件的功能,包括髮送附近、HTML頁面、圖片,並且可以有多個發送對象。
在實際使用中用到隊列、線程的技術,目的是防止發送郵件的過程阻礙主網站程式的運行。
具體代碼如下:
public bool SendEmail(out string message) { string str3; bool flag = false; message = string.Empty; this.MailMM.Priority = MailPriority.Normal; this.MailMM.BodyEncoding = Encoding.UTF8; this.MailMM.IsBodyHtml = this.IsBodyHtml;
//關於圖片
AlternateView htmlView = null; try { htmlView = AlternateView.CreateAlternateViewFromString(StrMailContent,null, "text/html"); for (int i = 0; i < aboImgSrcList.Count; i++) { try { LinkedResource logo = new LinkedResource(aboImgSrcList[i], "image/jpeg"); logo.ContentId = "imgSrc" + (i + 1); htmlView.LinkedResources.Add(logo); } catch (Exception EX) { } } MailMM.AlternateViews.Add(htmlView);
//關於附件
for (int i = 0; i < this.AttachmentsList.Count; i++) { string tempStr = this.AttachmentsList[i].ToString(); Attachment attach = new Attachment(tempStr); this.MailMM.Attachments.Add(attach); }
} catch (Exception) { } int count = 0; foreach (string str in this.arrMailTo.Keys) { try {this.MailMM.To.Add(new MailAddress(this.arrMailTo[str])); if (this.StrMailTitle != "") { this.MailMM.Subject = this.StrMailTitle; } this.MailMM.SubjectEncoding = Encoding.UTF8; if (this.MailMM.From == null) { this.MailMM.From = new MailAddress(this.StrSender, (this.StrSenderDesc == "") ? this.StrSender : this.StrSenderDesc, Encoding.UTF8); } SmtpClient client = new SmtpClient { DeliveryMethod = SmtpDeliveryMethod.Network, EnableSsl = false, Host = this.MailServer, Port = int.Parse(this.SmtpPort), Credentials = new NetworkCredential(this.StrSenderUser, this.StrSenderUserPass) }; client.Send(this.MailMM); MailMM.To.Clear(); flag = true; count++; } catch (SmtpException exception) { flag = false; message +="("+count+") "+DateTime.Now.ToString()+" "+ this.arrMailTo[str] + ": " + exception.Message + ";\r\n"; } catch (InvalidCastException exc) { flag = false; message += "(" + count + ") " + DateTime.Now.ToString() + " " + this.arrMailTo[str] + ": " + exc.Message + ";\r\n"; } catch (Exception ex) { flag = false; message += "(" + count + ") " + DateTime.Now.ToString() + " " + this.arrMailTo[str] + ": " + ex.Message + ";\r\n"; } } return flag; }
然後在global檔案中Application_Start的方法內開啟一下線程池,用於遍曆郵箱的隊列,如果隊列中有要發的郵件,則自動調用發送郵件的代碼進行郵件發送。
代碼如下:
1 protected void Application_Start(object sender, EventArgs e) 2 { 3 string basePath = AppDomain.CurrentDomain.BaseDirectory; 4 //step 2: start a thread to scan email queue(send email) 5 ThreadPool.QueueUserWorkItem((o) => 6 { 7 while (true) 8 { 9 try10 {11 if (EmailHelper.mailQueueLR.Count > 0)12 {13 lock (this)14 {15 if (EmailHelper.mailQueueLR.Count > 0)16 {17 int tempInt = EmailHelper.mailQueueLR.Dequeue();18 if (tempInt == null || tempInt == 0)19 {20 }21 else22 {23 //此處為建立一個郵件的具體代碼,根據實際情況編寫24 }25 catch (Exception ex)26 {27 //this.AddMsg(exception.Message);28 string str = ex.Message;29 str = str + "str";30 31 }32 }33 }34 }35 }36 else37 {38 Thread.Sleep(1000);39 }40 }41 catch (InvalidOperationException ioex)42 {43 continue;44 }45 catch (Exception ex)46 {47 continue;48 }49 50 }51 }, basePath);52 }View Code
實際開發中遇到的問題
1:在Ipad或者iphone裝置中出現了,接收到的郵件只顯示原始碼的情況。
原因:在發送郵件時,實際是發送了兩份,一份原始碼,一份可選內容,而在ipad中卻只顯示了原始碼
解決方案:把所有的HTML代碼都放到可變視圖(AlternateViews)中去,不再放到body裡面,
2:在郵件顯示時,出現了亂碼的情況。
原因:郵件內容編碼格式不統一,例如:本文內容跟標題編碼格式不統一
解決方案:統一編碼格式,所有字串都轉換成utf7編碼格式
ASP.NET發送郵件