c# SendMail發(fā)送郵件實例代碼
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace Common
{
/// <summary>
/// 基于system.net.mail發(fā)送郵件,支持附件
/// </summary>
public class NetSendMail
{
public static void MailSend(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent, IList<string> mailAttachments, System.Text.Encoding encoding, bool isBodyHtml)
{
MailMessage message = new MailMessage();
if (mailFrom.Trim() == "")
{
throw new Exception("發(fā)送郵件不可以為空");
}
message.From = new MailAddress(mailFrom);
if (mailTo.Count <= 0)
{
throw new Exception("接收郵件不可以為空");
}
foreach (string s in mailTo)
{
message.To.Add(new MailAddress(s));
}
if (mailCC.Count > 0)
{
foreach (string s in mailCC)
{
message.CC.Add(new MailAddress(s));
}
}
if (mailBCC.Count > 0)
{
foreach (string s in mailBCC)
{
message.Bcc.Add(new MailAddress(s));
}
}
message.Subject = mailTitle;
message.Body = mailContent;
message.BodyEncoding = encoding; //郵件編碼
message.IsBodyHtml = isBodyHtml; //內(nèi)容格式是否是html
message.Priority = MailPriority.High; //設(shè)置發(fā)送的優(yōu)先集
//附件
foreach (string att in mailAttachments)
{
message.Attachments.Add(new Attachment(att));
}
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = mailSmtpServer;
smtpClient.Credentials = new NetworkCredential(maiFromlAccount, mailFromPwd);
smtpClient.Timeout = 1000;
smtpClient.EnableSsl = false; //不使用ssl連接
smtpClient.Send(message);
}
public static void MailSendText(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent)
{
List<string> attList = new List<string>();
MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, false);
}
public static void MailSendHTML(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent)
{
List<string> attList = new List<string>();
MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, true);
}
}
}
相關(guān)文章
C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)
這篇文章主要介紹了C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)介紹,非常具有參考借鑒價值,特此分享到腳本之家平臺供大家學(xué)習(xí)2016-05-05
C#利用正則表達式實現(xiàn)獲取字符串中漢字的數(shù)量
這篇文章主要為大家詳細介紹了C#如何利用正則表達式實現(xiàn)獲取字符串中漢字的數(shù)量,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
C#使用DevExpress中的XtraCharts控件實現(xiàn)圖表
這篇文章介紹了C#使用DevExpress中的XtraCharts控件實現(xiàn)圖表的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C# TabControl手動觸發(fā)DrawItem的實現(xiàn)
本文主要介紹了C# TabControl手動觸發(fā)DrawItem的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
解決C# winForm自定義鼠標(biāo)樣式的兩種實現(xiàn)方法詳解
本篇文章是對在C#中winForm自定義鼠標(biāo)樣式的兩種實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05
c#調(diào)用arcgis地圖rest服務(wù)示例詳解(arcgis地圖輸出)
ArcGIS REST API提供了簡單、開放的接口來訪問和使用ArcGIS Server發(fā)布的服務(wù)。使用ArcGIS REST API通過URL可以獲取和操作每一個服務(wù)中的所有資源和操作2013-12-12

