zoukankan      html  css  js  c++  java
  • 阿里云 服务器 邮件发送

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

     解决办法是向阿里云申请解封25端口,或者更换端口,建议使用587端口(有的说465可用但经过测试不可用)

    using System.Configuration;
    using System.Collections.Specialized;
    using System.IO;
    using System.Net.Mail;
    public string BulidFile(string tempPath, NameValueCollection values)
            {
                string prefix = "[$";
                string postfix = "]";
                StreamReader reader = null;
                string template = string.Empty;
                try
                {
                    reader = new StreamReader(tempPath);
                    template = reader.ReadToEnd();
                    reader.Close();
                    if (values != null)
                    {
                        foreach (string key in values.AllKeys)
                        {
                            template = template.Replace(string.Format("{0}{1}{2}", prefix, key, postfix), values[key]);
                        }
                    }
                }
                catch
                {
    
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }
                return template;
    
            }

    Email文件:

    Hello,
    [$NAME]
    [$URL]

    配置文件:

    <appSettings>
        <!-- 发送地址-->
        <add key="sendFrom" value="111111111@qq.com" />
        <!-- 发送人的名字-->
        <add key="sendUserName" value="发送人姓名" />
        <!-- 邮件标题-->
        <add key="sendTitle" value="EmailTitle" />
        <!-- 邮箱用户名-->
        <add key="userName" value="111111111@qq.com" />
        <!-- 邮箱密码注意使用授权码-->
        <add key="passWord" value="bccdbrpveswhbghj" />
        <!-- 邮箱服务器地址-->
        <add key="smtpServer" value="smtp.qq.com" />
      </appSettings>
     1 public void SendEmail(string recName)
     2         {
     3             string templetpath = Server.MapPath("../../Resource/EmailTemplate/Email.txt");
     4             NameValueCollection myCol = new NameValueCollection();
     5             string Url ="";
     6             string Name = "";
     7             myCol.Add("NAME", Name);
     8             myCol.Add("URL", Url);
     9             string mailBody = BulidFile(templetpath, myCol);
    10             string sendFrom = ConfigurationManager.AppSettings["sendFrom"]; //生成一个发送地址
    11             string sendUserName = ConfigurationManager.AppSettings["sendUserName"];//发送人的名字          
    12             string sendTitle = ConfigurationManager.AppSettings["sendTitle"];//发送邮件标题
    13             string username = ConfigurationManager.AppSettings["userName"];//发送邮箱用户名
    14             string passwd = ConfigurationManager.AppSettings["passWord"];//发送邮箱密码
    15             string smtpServer = ConfigurationManager.AppSettings["smtpServer"];//发送邮箱密码
    16             MailMessage msg = new MailMessage();
    17             msg.To.Add(recName);
    18             /*   
    19             * msg.To.Add("b@b.com");   
    20             * msg.To.Add("b@b.com");   
    21             * msg.To.Add("b@b.com");可以发送给多人   
    22             */
    23             /*   
    24             * msg.CC.Add("c@c.com");   
    25             * msg.CC.Add("c@c.com");可以抄送给多人   
    26             */
    27             /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
    28             msg.From = new MailAddress(sendFrom);
    29             msg.Subject = sendTitle;//邮件标题    
    30             msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码    
    31             msg.Body = mailBody;//邮件内容    
    32             msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
    33             msg.Priority = System.Net.Mail.MailPriority.High;//邮件优先级    
    34             msg.IsBodyHtml = false;//是否是HTML邮件    
    35             SmtpClient client = new SmtpClient();
    36             //注意一下三行代码
    37             client.EnableSsl = true;
    38             client.UseDefaultCredentials = false;
    39             client.DeliveryMethod = SmtpDeliveryMethod.Network;
    40             //指定发件人的邮件地址和密码以验证发件人身份默认端口为25
    41             client.Port = 587;
    42             client.Credentials = new System.Net.NetworkCredential(sendFrom, passwd);
    43             client.Host = smtpServer;
    44             try
    45             {
    46                 client.Send(msg);  --异步可使用SendAsync
    48             }
    49             catch (System.Net.Mail.SmtpException ex)
    50             {
    51 
    52             }
    53         }

  • 相关阅读:
    p div span 标签的区别 对HTMl基础进一步理解
    js 处理 ie和firefox window.frames 兼容问题(转)
    javascript Hover事件和 mouseover 事件以及mouseout事件的联系
    JavaScript程序执行顺序问题总结(转)
    【转】 document.frames()与document.frames[]的区别
    XHTML 与 HTML 之间的差异
    root 用户和 sudo 命令
    ubuntu创建、删除文件及文件夹,强制清空回收站方法
    第二代DNA测序技术
    /usr/bin/ld: final link failed: Illegal seek
  • 原文地址:https://www.cnblogs.com/heibai-ma/p/10913466.html
Copyright © 2011-2022 走看看