• C#SMTP发送邮件在阿里云服务器上需要注意的问题


      使用SMTP(简单邮件传输协议)发送邮件一般都是使用25端口,而阿里云服务器为了安全是将25端口封禁的,会出现在本机测试发送邮件功能正常,但是部署到服务器上却发送失败的情况。

      解决办法是向阿里云申请解封25端口,或者更换端口,可以使用465或者587端口。

      代码如下:

     1 /// <summary>
     2     /// 发送邮件
     3     /// </summary>
     4     /// <param name="bodyStr">邮件内容</param>
     5     /// <param name="title">邮件标题</param>
     6     /// <returns>返回结果,成功与否</returns>
     7     public static string SendMail(string bodyStr, string title)
     8     {
     9         SmtpClient smtpClient = new SmtpClient();
    10         smtpClient.EnableSsl = true;
    11         smtpClient.UseDefaultCredentials = false;
    12         smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    13         smtpClient.Host = "smtp.qq.com";
    14         smtpClient.Port = 587;//改用587端口
    15         smtpClient.Credentials = new System.Net.NetworkCredential("000@qq.com", "abcdefg");
    16         //密码不是QQ密码,是qq账户设置里面的POP3/SMTP服务生成的key
    17         MailMessage mailMessage = new MailMessage("000@qq.com", "xxx@xxx.com");//from(发出邮箱)和to(目标邮箱)
    18         mailMessage.Subject = title;//邮件标题 
    19         mailMessage.Body = bodyStr;//邮件内容 
    20         mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码  
    21         mailMessage.IsBodyHtml = true;//设置为HTML格式  
    22         mailMessage.Priority = MailPriority.High;//优先级  
    23         mailMessage.To.Add("xxx@xxx.com");
    24         mailMessage.To.Add("aaa@xxx.com");
    25         mailMessage.To.Add("bbb@xxx.com");
    26         mailMessage.To.Add("ccc@xxx.com");//可以发送给多个人
    27         try
    28         {
    29             smtpClient.Send(mailMessage);
    30             return "发送成功";
    31         }
    32         catch (Exception e)
    33         {
    34             return "发送失败" + e.InnerException.Message;
    35         }
    36     }
    //qq911322692 欢迎添加 一起讨论编程学习经验
  • 相关阅读:
    [Algorithms] Counting Sort
    [LeetCode] Sort Colors
    [LeetCode] Contains Duplicate III
    [LeetCode] Contains Duplicate
    [LeetCode] Two Sum II
    [LeetCode] Linked List Cycle II
    [LeetCode] Linked List Cycle
    [LeetCode] Longest Palindromic Substring
    [LeetCode] Two Sum
    [LeetCode] Rectangle Area
  • 原文地址:https://www.cnblogs.com/xinwenpeng/p/9818640.html
Copyright © 2020-2023  润新知