在mojoportal项目中发邮件使用的是dotnetopenmail
生活随笔
收集整理的這篇文章主要介紹了
在mojoportal项目中发邮件使用的是dotnetopenmail
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
[翻譯]開源發(fā)送郵件組件dotnetopenmail使用介紹
在mojoportal項(xiàng)目中發(fā)郵件使用的是dotnetopenmail介紹
發(fā)郵件是非常簡(jiǎn)單的,不管用System.web.mail [ASP.NET 1.1]或System.Net.mail [ASP.NET 2.0],但是還是有其他第三方組件可以被使用,比如DotNetOpenMail.他可以使你用最少的代碼創(chuàng)建文本,html和帶附件的郵件通過 smtp服務(wù)器發(fā)送.
你可以從這個(gè)網(wǎng)址得到組件:http://dotnetopenmail.sourceforge.net/
使用組件發(fā)送郵件
只要將DotNetOpenMail.dll引用到項(xiàng)目,并且寫下下面的代碼
//Using?Namespaces?that?are?to?be?included
using?DotNetOpenMail;
using?DotNetOpenMail.Logging;
using?DotNetOpenMail.Encoding;
using?DotNetOpenMail.Resources;
using?DotNetOpenMail.SmtpAuth;
/*創(chuàng)建一個(gè)郵件對(duì)象?*/
EmailMessage?emailMessage?=?new?EmailMessage();
/*添加發(fā)件人的郵箱地址*/
emailMessage.FromAddress?=?new?EmailAddress("senderID@domainName.com");
/*添加收件人的郵箱地址*/
emailMessage.AddToAddress(new?EmailAddress("receiverID@domainName.com"));
/*添加郵件主題*/
emailMessage.Subject?=?"Subject?of?the?Message"
/*設(shè)置郵件內(nèi)容通過html附件*/
emailMessage.HtmlPart?=?new?HtmlAttachment("<html><body><p>Write?Down?some?BODY-Text?here</p></body></html>");
/*初始化smtp服務(wù)器?*/
SmtpServer?smtpServer=new?SmtpServer("smtpout.domainName.com");
/*?提供smtp服務(wù)器上有效的用戶名和密碼*/
smtpServer.SmtpAuthToken=new?SmtpAuthToken("webmaster@domainName.com",?"password");
/*設(shè)置內(nèi)容類型*/
emailMessage.ContentType?=?"TEXT/HTML"
/*發(fā)送郵件*/
emailMessage.Send(smtpServer);
Response.Write?("?Email?Successfully?Sent");
為郵件添加附件
/*?創(chuàng)建一個(gè)MemoryStream對(duì)象*/
MemoryStream?memStream?=?new?MemoryStream();
/*?創(chuàng)建一個(gè)StreamWriter?對(duì)象*/
StreamWriter?writer?=?new?StreamWriter(memStream);
/*?在StreamWriter對(duì)象中寫入一些內(nèi)容*/
writer.WriteLine("?HELLO?WORLD?!?");
writer.Flush();
/*?讓memory?stream指回文件的開始?*/
memStream.Seek(0,?SeekOrigin.Begin);
/*創(chuàng)建一個(gè)文件附件且將memory?stream中的內(nèi)容添加進(jìn)來*/
FileAttachment?attachment?=?new?FileAttachment(new?StreamReader(memStream));
/*為文件附件設(shè)置名稱,內(nèi)容類型和字符集參數(shù)*/
attachment.FileName?=?"Hello.txt"
attachment.CharSet?=?System.Text.Encoding.ASCII;
attachment.ContentType?=?"text/plain"
/*將附件加入郵件中*/
emailMessage.AddMixedAttachment(attachment);
轉(zhuǎn)載于:https://www.cnblogs.com/window5549-accp/archive/2009/12/11/1621808.html
總結(jié)
以上是生活随笔為你收集整理的在mojoportal项目中发邮件使用的是dotnetopenmail的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海康Ehome协议服务端搭建
- 下一篇: .NET建模