c# SendMail發(fā)送郵件實(shí)例代碼
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ù)介紹,非常具有參考借鑒價(jià)值,特此分享到腳本之家平臺(tái)供大家學(xué)習(xí)2016-05-05C#利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量
這篇文章主要為大家詳細(xì)介紹了C#如何利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01C#中Winfrom默認(rèn)輸入法的設(shè)置方法
這篇文章主要介紹了C#中Winfrom默認(rèn)輸入法的設(shè)置方法,以實(shí)例形式較為詳細(xì)的分析了C#中輸入法設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-05-05積累Visual Studio 常用快捷鍵的動(dòng)畫演示
在代碼開發(fā)過程中,頻繁的使用鍵盤、鼠標(biāo)操作非常麻煩,影響程序的開發(fā)效率。如何操作能用鍵盤來操作,那就節(jié)省時(shí)間了。下面小編把我平時(shí)積累的有關(guān)visul studio 常用快捷鍵的動(dòng)畫演示分享給大家,僅供大家參考2015-10-10C#使用DevExpress中的XtraCharts控件實(shí)現(xiàn)圖表
這篇文章介紹了C#使用DevExpress中的XtraCharts控件實(shí)現(xiàn)圖表的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C# TabControl手動(dòng)觸發(fā)DrawItem的實(shí)現(xiàn)
本文主要介紹了C# TabControl手動(dòng)觸發(fā)DrawItem的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02解決C# winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法詳解
本篇文章是對(duì)在C#中winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05c#調(diào)用arcgis地圖rest服務(wù)示例詳解(arcgis地圖輸出)
ArcGIS REST API提供了簡單、開放的接口來訪問和使用ArcGIS Server發(fā)布的服務(wù)。使用ArcGIS REST API通過URL可以獲取和操作每一個(gè)服務(wù)中的所有資源和操作2013-12-12