欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.Net使用SmtpClient发送邮件,报错:不允许使用邮箱名称。 服务器响应为: xxxw

程序员文章站 2022-07-04 17:49:27
从网上找了一个封装.Net的Smtp邮件发送的类 [csharp] class MailSender  {     ...

从网上找了一个封装.Net的Smtp邮件发送的类


[csharp]
class MailSender 

    private MailMessage mailMessage; 
    private SmtpClient smtpClient; 
    private string password;//发件人密码   
     
    /// <summary>    
    /// 处审核后类的实例    
    /// </summary>    
    /// <param name="To">收件人地址</param>    
    /// <param name="From">发件人地址</param>    
    /// <param name="Body">邮件正文</param>    
    /// <param name="Title">邮件的主题</param>    
    /// <param name="Password">发件人密码</param>    
    public MailSender(string To, string From, string Body, string Title, string Password) 
    { 
        mailMessage = new MailMessage(); 
        mailMessage.To.Add(To); 
        mailMessage.From = new System.Net.Mail.MailAddress(From); 
        mailMessage.Subject = Title; 
        mailMessage.Body = Body; 
        mailMessage.IsBodyHtml = true; 
        mailMessage.BodyEncoding = System.Text.Encoding.UTF8; 
        mailMessage.Priority = System.Net.Mail.MailPriority.Normal; 
        this.password = Password; 
    } 
    /// <summary>    
    /// 添加附件    
    /// </summary>    
    public void Attachments(string Path)   
    {   
        string[] path = Path.Split(',');   
        Attachment data;   
        ContentDisposition disposition;   
        for (int i = 0; i < path.Length; i++)   
        {   
            data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件    
            disposition = data.ContentDisposition;   
            disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期    
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期    
            disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期    
            mailMessage.Attachments.Add(data);//添加到附件中    
        }   
    } 
    /// <summary>    
    /// 异步发送邮件    
    /// </summary>    
    /// <param name="CompletedMethod"></param>    
    public void SendAsync(SendCompletedEventHandler CompletedMethod) 
    { 
        if (mailMessage != null) 
        { 
            smtpClient = new SmtpClient(); 
            smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据    
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
            smtpClient.Host = "smtp." + mailMessage.From.Host; 
            smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件    
            smtpClient.SendAsync(mailMessage, mailMessage.Body); 
        } 
    } 
    /// <summary>    
    /// 发送邮件    
    /// </summary>    
    public void Send() 
    { 
        if (mailMessage != null) 
        { 
            smtpClient = new SmtpClient(); 
            smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据    
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
            smtpClient.Host = "smtp." + mailMessage.From.Host; 
            smtpClient.Send(mailMessage); 
        } 
    }   

class MailSender
{
 private MailMessage mailMessage;
 private SmtpClient smtpClient;
 private string password;//发件人密码
 
 /// <summary> 
 /// 处审核后类的实例 
 /// </summary> 
 /// <param name="To">收件人地址</param> 
 /// <param name="From">发件人地址</param> 
 /// <param name="Body">邮件正文</param> 
 /// <param name="Title">邮件的主题</param> 
 /// <param name="Password">发件人密码</param> 
 public MailSender(string To, string From, string Body, string Title, string Password)
 {
  mailMessage = new MailMessage();
  mailMessage.To.Add(To);
  mailMessage.From = new System.Net.Mail.MailAddress(From);
  mailMessage.Subject = Title;
  mailMessage.Body = Body;
  mailMessage.IsBodyHtml = true;
  mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
  mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
  this.password = Password;
 }
 /// <summary> 
 /// 添加附件 
 /// </summary> 
 public void Attachments(string Path) 
 { 
  string[] path = Path.Split(','); 
  Attachment data; 
  ContentDisposition disposition; 
  for (int i = 0; i < path.Length; i++) 
  { 
   data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件 
   disposition = data.ContentDisposition; 
   disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期 
   disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期 
   disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期 
   mailMessage.Attachments.Add(data);//添加到附件中 
  } 
 }
 /// <summary> 
 /// 异步发送邮件 
 /// </summary> 
 /// <param name="CompletedMethod"></param> 
 public void SendAsync(SendCompletedEventHandler CompletedMethod)
 {
  if (mailMessage != null)
  {
   smtpClient = new SmtpClient();
   smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据 
   smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
   smtpClient.Host = "smtp." + mailMessage.From.Host;
   smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件 
   smtpClient.SendAsync(mailMessage, mailMessage.Body);
  }
 }
 /// <summary> 
 /// 发送邮件 
 /// </summary> 
 public void Send()
 {
  if (mailMessage != null)
  {
   smtpClient = new SmtpClient();
   smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据 
   smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
   smtpClient.Host = "smtp." + mailMessage.From.Host;
   smtpClient.Send(mailMessage);
  }
 } 
}
使用某些邮箱的时候会报错(如sohu):不允许使用邮箱名称,服务器响应为: xxx。需要设置单独的用户名,故修改了下,并做了一些优化。如下:

 

 


[csharp] 
class MailSender 

    private readonly MailMessage mail; 
    private readonly string password;//发件人密码   
    private readonly string user; 
 
    /// <summary>    
    /// 处审核后类的实例    
    /// </summary>    
    /// <param name="to">收件人地址</param>    
    /// <param name="from">发件人地址</param>    
    /// <param name="body">邮件正文</param>    
    /// <param name="subject">邮件的主题</param>    
    /// <param name="sendAccount">发件人账号</param>  
    /// <param name="sendPassword">发件人密码</param>    
    public MailSender(string to, string from, string body, string subject, string sendAccount, string sendPassword) 
    { 
        mail = new MailMessage(); 
        mail.To.Add(to); 
        mail.From = new MailAddress(from); 
        mail.Subject = subject; 
        mail.Body = body; 
        mail.IsBodyHtml = true; 
        mail.BodyEncoding = System.Text.Encoding.UTF8; 
        mail.Priority = MailPriority.Normal; 
        this.password = sendPassword; 
        this.user = sendAccount; 
    } 
    /// <summary>    
    /// 添加附件    
    /// </summary>    
    public void Attachments(string path)   
    {   
        string[] pathes = path.Split(',');   
        for (int i = 0; i < pathes.Length; i++)   
        {   
            var data = new Attachment(pathes[i], MediaTypeNames.Application.Octet);//实例化附件    
            var disposition = data.ContentDisposition;   
            disposition.CreationDate = System.IO.File.GetCreationTime(pathes[i]);//获取附件的创建日期    
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(pathes[i]);//获取附件的修改日期    
            disposition.ReadDate = System.IO.File.GetLastAccessTime(pathes[i]);//获取附件的读取日期    
            mail.Attachments.Add(data);//添加到附件中    
        }   
    } 
    /// <summary>    
    /// 异步发送邮件    
    /// </summary>    
    /// <param name="CompletedMethod"></param>    
    public void SendAsync(SendCompletedEventHandler CompletedMethod) 
    { 
        if (mail != null) 
        { 
            var smtpClient = new SmtpClient(); 
            smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据    
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; 
            smtpClient.Host = "smtp." + mail.From.Host; 
            smtpClient.SendCompleted += CompletedMethod;//注册异步发送邮件完成时的事件    
            smtpClient.SendAsync(mail, mail.Body); 
        } 
    } 
    /// <summary>    
    /// 发送邮件    
    /// </summary>    
    public void Send() 
    { 
        if (mail != null) 
        { 
            var smtpClient = new SmtpClient(); 
            smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据    
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; 
            smtpClient.Host = "smtp." + mail.From.Host; 
            smtpClient.Send(mail); 
        } 
    }   

    class MailSender
    {
        private readonly MailMessage mail;
        private readonly string password;//发件人密码
        private readonly string user;

        /// <summary> 
        /// 处审核后类的实例 
        /// </summary> 
        /// <param name="to">收件人地址</param> 
        /// <param name="from">发件人地址</param> 
        /// <param name="body">邮件正文</param> 
        /// <param name="subject">邮件的主题</param> 
        /// <param name="sendAccount">发件人账号</param>
        /// <param name="sendPassword">发件人密码</param> 
        public MailSender(string to, string from, string body, string subject, string sendAccount, string sendPassword)
        {
            mail = new MailMessage();
            mail.To.Add(to);
            mail.From = new MailAddress(from);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = true;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.Priority = MailPriority.Normal;
            this.password = sendPassword;
            this.user = sendAccount;
        }
        /// <summary> 
        /// 添加附件 
        /// </summary> 
        public void Attachments(string path) 
        { 
            string[] pathes = path.Split(','); 
            for (int i = 0; i < pathes.Length; i++) 
            { 
                var data = new Attachment(pathes[i], MediaTypeNames.Application.Octet);//实例化附件 
                var disposition = data.ContentDisposition; 
                disposition.CreationDate = System.IO.File.GetCreationTime(pathes[i]);//获取附件的创建日期 
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(pathes[i]);//获取附件的修改日期 
                disposition.ReadDate = System.IO.File.GetLastAccessTime(pathes[i]);//获取附件的读取日期 
                mail.Attachments.Add(data);//添加到附件中 
            } 
        }
        /// <summary> 
        /// 异步发送邮件 
        /// </summary> 
        /// <param name="CompletedMethod"></param> 
        public void SendAsync(SendCompletedEventHandler CompletedMethod)
        {
            if (mail != null)
            {
                var smtpClient = new SmtpClient();
                smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据 
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.Host = "smtp." + mail.From.Host;
                smtpClient.SendCompleted += CompletedMethod;//注册异步发送邮件完成时的事件 
                smtpClient.SendAsync(mail, mail.Body);
            }
        }
        /// <summary> 
        /// 发送邮件 
        /// </summary> 
        public void Send()
        {
            if (mail != null)
            {
                var smtpClient = new SmtpClient();
                smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据 
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.Host = "smtp." + mail.From.Host;
                smtpClient.Send(mail);
            }
        } 
    }