dotnetopenmail的使用

dotnetopenmail使用介绍:

在mojoportal项目中发邮件使用的是dotnetopenmail

介绍
发邮件是非常简单的,不管用System.web.mail [ASP.NET 1.1 ]或System.Net.mail [ASP.NET 2.0 ],但是还是有其他第三方组件可以被使用,比如DotNetOpenMail.他可以使你用最少的代码创建文本,html和带附件的邮件通过 smtp服务器发送.

你可以从这个网址得到组件:http: //dotnetop enmail.sourceforge.net/

使用组件发送邮件

只要将DotNetOpenMail.dll引用到项目,并且写下下面的代码

//Using Namespaces that are to be included

using DotNetOpenMail;
using DotNetOpenMail.Logging;
using DotNetOpenMail.Encoding;
using DotNetOpenMail.Resources;
using DotNetOpenMail.SmtpAuth;

/*创建一个邮件对象 */
EmailMessage emailMessage = new EmailMessage();
/*添加发件人的邮箱地址*/
emailMessage.FromAddress = new EmailAddress("[email protected]" );
/*添加收件人的邮箱地址*/
emailMessage.AddToAddress(new EmailAddress("[email protected]" ));
/*添加邮件主题*/
emailMessage.Subject = "Subject of the Message"

/*设置邮件内容通过html附件*/
emailMessage.HtmlPart = new HtmlAttachment("<html><body><p>Write Down some BODY-Text here</p></body></html>" );
/*初始化smtp服务器 */
SmtpServer smtpServer=new SmtpServer("smtpout.domainName.com" );

/* 提供smtp服务器上有效的用户名和密码*/
smtpServer.SmtpAuthToken=new SmtpAuthToken("[email protected]" , "password" );
/*设置内容类型*/
emailMessage.ContentType = "TEXT/HTML"
/*发送邮件*/
emailMessage.Send(smtpServer);
Response.Write (" Email Successfully Sent" );

为邮件添加附件

/* 创建一个MemoryStream对象*/
MemoryStream memStream = new MemoryStream();
/* 创建一个StreamWriter 对象*/
StreamWriter writer = new StreamWriter(memStream);
/* 在StreamWriter对象中写入一些内容*/
writer.WriteLine(" HELLO WORLD ! " );
writer.Flush();
/* 让memory stream指回文件的开始 */
memStream.Seek(0 , SeekOrigin.Begin);

/*创建一个文件附件且将memory stream中的内容添加进来*/
FileAttachment attachment = new FileAttachment(new StreamReader(memStream));
/*为文件附件设置名称,内容类型和字符集参数*/
attachment.FileName = "Hello.txt"
attachment.CharSet = System.Text.Encoding.ASCII;
attachment.ContentType = "text/plain"
/*将附件加入邮件中*/
emailMessage.AddMixedAttachment(attachment);

你可能感兴趣的:(html,Stream,服务器,asp.net,NameSpaces)