.NET发送邮件时注意的问题~~

来源:互联网 发布:pid算法原理及调整规律 编辑:程序博客网 时间:2024/06/05 16:11
 
"不允许使用邮箱名称。 服务器响应为:   You   are   not   authorized   to   send   mail,   au...  "

不使用配置文件发送邮件时遇到如上问题,请按如下方法解决:

smtpClient.UseDefaultCredentials = true;

这一句去掉

加上下面两句

NetworkCredential credential = new NetworkCredential(username, password);
   smtpClient.Credentials = credential;

即可.

 

使用配置文件发送邮件:

<system.net>
    
<mailSettings>
      
<smtp deliveryMethod="Network" from="dingfeng_wu@163.com">
        
<network host="smtp.163.com" userName="dingfeng_wu" password="**********" port="25" defaultCredentials="false"/>
      
</smtp>
    
</mailSettings>
  
</system.net>

发送代码:

 

public void MailTest()
        {
            
string subject = "test";
            
string body = "hello";
            SmtpSection smtpSec 
= (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
            
using (MailMessage message = new MailMessage(smtpSec.From, "dingfeng_wu@sina.com.cn", subject, body))
            {
                System.Net.Mail.SmtpClient mailClient 
= new System.Net.Mail.SmtpClient();
                mailClient.Send(message);
            }
        }

 

 

原创粉丝点击