在阿里云服务器发邮件

     在本地调试可以正常发送邮件,在服务器上发送失败

  原因25端口被阿里云禁用,不能被开启,只能尝试用465 端口采用加密方式发送邮件。

     然而 使用 smtp.163.com 的用 465端口发送还是失败,用自己网站的邮箱发送也是失败的报554错误啥的

     最后用QQ的邮箱成功了!

Demo:

代码:

public class MailHelper
    {
        public static readonly string MailName = "邮件中心";
        public static readonly string body = "XXXXX";

        public static string send(string toMail, string MailServer, string MailUserName, string MailPassword, int Port, bool EnableSsl)
        {
            try
            {
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl = EnableSsl;
                smtpClient.Host = MailServer;
                smtpClient.Port = Port;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = (ICredentialsByHost)new NetworkCredential(MailUserName, MailPassword);
                MailMessage message = new MailMessage()
                {
                    Priority = MailPriority.Normal,
                    From = new MailAddress(MailUserName, "XXXX", Encoding.UTF8),
                    Subject = "主题",
                    SubjectEncoding = Encoding.UTF8,
                    To = {
                        toMail
                    }
                };
                message.SubjectEncoding = Encoding.GetEncoding(936);
                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.GetEncoding(936);
                message.Body = "<font color='red'>" + MailHelper.body + "</font><br>";
                smtpClient.Send(message);
                return "邮件发送成功";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }


        public static string send1(string toMail, string MailServer, string MailUserName, string MailPassword, int Port, bool EnableSsl)
        {
            System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
            //邮件主题
            mmsg.Subject = "hello, world";
            mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
            //邮件正文
            mmsg.Body = "hello, world。哈哈哈哈哈哈,听说会被屏蔽掉的";
            //正文编码
            mmsg.BodyEncoding = Encoding.UTF8;
            //优先级
            mmsg.Priority = System.Web.Mail.MailPriority.Normal;
            //发件者邮箱地址
            mmsg.From = MailUserName;
            //收件人收箱地址
            mmsg.To = toMail;

            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //用户名
            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailUserName);
            //密码
            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailPassword);
            //端口 
            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", Port);
            //是否ssl
            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", EnableSsl);
            //Smtp服务器
            System.Web.Mail.SmtpMail.SmtpServer = MailServer;

            try
            {
                System.Web.Mail.SmtpMail.Send(mmsg);
                return "发送成功";
            }
            catch (Exception ex)
            {
                return "发送失败,失败原因:" + ex.Message;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_32688731/article/details/81502281