听棠.NET

用积极乐观的心态,面对压力
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

轻松实现用SMTP发送需用户验证的邮件!

Posted on 2005-03-04 10:01  听棠.NET  阅读(9148)  评论(45编辑  收藏  举报
 以前我也用过STMP发送Email,但对于需要用户验证的邮件发送一直不成功,因为采用的是Jmail等第三方组件,这需要安装第三方控件,确实比较麻烦。
 还有在网上也找了一些文章,都提到使用CDO组件,但也需要添加引用等。今天发现一个更简单的,不需要添加组件,还是使用SMTP发送即可:
1) 
using System.Web.Mail;
2)
 
MailMessage mail = new MailMessage();
       mail.To 
= "me@mycompany.com";
       mail.From 
= "you@yourcompany.com";
       mail.Subject 
= "this is a test email.";
       mail.Body 
= "Some text goes here";
          mail.BodyFormat = MailFormat.Html;//设置为HTML格式 
         //设置为需要用户验证
      mail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");  
         //设置验证用户名(把my_username_here改为你的验证用户名)
      mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername""my_username_here"); 
         //设置验证密码(把password改为你的验证密码)
      mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword""password"); 
      SmtpMail.SmtpServer 
= "mail.mycompany.com";  //邮件服务器地址
      SmtpMail.Send( mail );

好了,就这么简单,只要在mail.Fields里Add三个设置就可以了。
 我测试通过了!!