smtp發(fā)送帶附件的郵件代碼分享
該方式直接保存為HTML文件,也可以是文本文件,其它格式效果不是很好
MailMessage mmsg = new MailMessage();
mmsg.Subject = "郵件標(biāo)題";
mmsg.Body = "郵件內(nèi)容";
mmsg.To.Add("accept@qq.com");//接收郵箱
byte[] bytes = System.Text.Encoding.Default.GetBytes
(@"<table><tr><td width=150>1234567891234567
</td><td width=80>12345678</td></tr></table>");
MemoryStream ms = new MemoryStream(bytes);
ContentType ct = new ContentType();
//附件文件類型
ct.MediaType = MediaTypeNames.Text.Html;
//附件名稱,可以是其它后綴名
ct.Name = "附件名稱" + DateTime.Now.ToString() + ".html";
mmsg.Attachments.Add(new Attachment(ms, ct));
//SMTP簡單郵件協(xié)議
System.Net.Mail.SmtpClient sc
= new System.Net.Mail.SmtpClient();
sc.Host = "127.0.0.1";//主機(jī)地址
sc.Port = 25;//端口
//發(fā)送郵箱賬號和密碼
sc.Credentials =
new System.Net.NetworkCredential("account", "password");
//發(fā)送郵箱
mmsg.From = new MailAddress("account@qq.com");
sc.Send(mmsg);
//釋放流資源
ms.Close();
ms.Dispose();
另附上一個(gè).net使用smtp發(fā)送帶附件郵件的實(shí)例
public static void sendEmail(string toAddress, string emailbody)
{
var fromAddress = ConfigurationManager.AppSettings["EmailAddress"];
string fromPassword = ConfigurationManager.AppSettings["EmailPassword"].ToString();
const string subject = "Job Recommendation";
var smtp = new SmtpClient
{
Host = ConfigurationManager.AppSettings["SmtpServer"].ToString(),
Port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress, subject, HttpUtility.HtmlEncode(emailbody)))
{
smtp.Send(message);
}
}
<add key="EmailAddress" value="**********@gmail.com"/>//Email Address
<add key="EmailPassword" value="*********"/> //Emial PWD
<add key="SmtpServer" value="smtp.gmail.com"/>
<add key="SmtpPort" value="587"/>
<--帶附件版本->
var fromAddress = "allenyinj@gmail.com";
string fromPassword = "yj1989120";
const string subject = "CV";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress, fromPassword)
};
MailMessage email=new MailMessage(fromAddress, "allen.yin.jun@gmail.com");
email.Subject = "INLINE attachment TEST";
email.IsBodyHtml = true;
string attachmentPath = "C:\\3.jpeg";
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
//inline.ContentId = "1";
//inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
email.Attachments.Add(inline);
email.Body = "test";
smtp.Send(email);
email.Dispose();
//如果沒有路徑,用Stream
Attachment letter = new Attachment(FileUploadLetter.FileContent, FileUploadLetter.PostedFile.ContentType);
letter.ContentDisposition.Inline = true;
letter.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
//inline.ContentId = "1";
letter.ContentType.MediaType = FileUploadLetter.PostedFile.ContentType;
letter.ContentType.Name = Path.GetFileName(FileUploadLetter.PostedFile.FileName);
letter.Name = Path.GetFileName(FileUploadLetter.PostedFile.FileName);
相關(guān)文章
ASP.NET使用Subtract方法獲取兩個(gè)日期之間的天數(shù)
本節(jié)主要介紹了ASP.NET使用Subtract方法獲取兩個(gè)日期之間的天數(shù),需要的朋友可以參考下2014-08-08如何給ASP.NET Core Web發(fā)布包做減法詳解
在ASP.Net中可以使用打包與壓縮這兩種技術(shù)來提高Web應(yīng)用程序頁面加載的性能。下面這篇文章主要給大家介紹了關(guān)于如何給ASP.NET Core Web發(fā)布包做減法的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧2018-06-06asp.net類庫中添加WebService引用出現(xiàn)問題解決方法
在Web項(xiàng)目內(nèi)添加WebService的引用是件很簡單的事情,不過對于一些新手朋友來說,就沒有那么簡單了,因?yàn)樵谔砑拥倪^程中總會(huì)遇到一些困難,接下來詳細(xì)介紹如何解決,感興趣的你可不要錯(cuò)過了啊2013-02-02asp.net Repeater分頁實(shí)例(PageDataSource的使用)
Asp.net提供了三個(gè)功能強(qiáng)大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分頁功能。相對DataGrid,DataList和Repeater控件具有更高的樣式自定義性,所以很多時(shí)候我們喜歡使用DataList或Repeater控件來顯示數(shù)據(jù)2013-04-04ASP.NET Core環(huán)境變量和啟動(dòng)設(shè)置的配置教程
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core環(huán)境變量和啟動(dòng)設(shè)置的配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07window.showModalDialog兩次加載問題清除緩存方法
window.showModalDialog兩次加載問題:你第一次打開窗口后,第二次瀏覽器沒有從服務(wù)器端取數(shù)據(jù),而直接找到了你已經(jīng)下載的文件2013-08-08MVC+EasyUI+三層新聞網(wǎng)站建立 建站準(zhǔn)備工作(一)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第一篇,建站的準(zhǔn)備工作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07使用Supervisor守護(hù)ASP.NET?Core應(yīng)用程序進(jìn)程
這篇文章介紹了使用Supervisor守護(hù)ASP.NET?Core應(yīng)用程序進(jìn)程的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03