• System.net.mail发送电子邮件


    之前做的实现发送邮件的功能,基于System.net.mail,在本地测试是可以发送邮件的,发布到服务器上发送不了邮件,后来发现STMP默认使用25端口收发邮件,服务器封掉25了端口,导致发送邮件失败。

    后来改用了587端口发送电子邮件。

    public static bool SendEmail587(string email, string validateCode)
            {
                bool result = true;
                try
                {
                    string eMailSmtpClient = CommFunction.GetAppSetting("EMailSmtpClient"); //邮箱服务地址
                    string eMailAccount = CommFunction.GetAppSetting("EMailAccount");//邮箱账号
                    string eMailPassword = CommFunction.GetAppSetting("EMailPassword");//邮箱密码
                    string eMailName = CommFunction.GetAppSetting("EMailName");
                    string eMailSubject = CommFunction.GetAppSetting("EMailSubjectValidateCode");
                    string eMailBodyChinese = CommFunction.GetAppSetting("eMailBodyValidateCode_Chinese");
                    string eMailBodyEnglish = CommFunction.GetAppSetting("eMailBodyValidateCode_English");

                    MailMessage msg = new MailMessage();
                    msg.To.Add(email);
                    msg.From = new MailAddress(eMailAccount, eMailName, Encoding.UTF8);
                    msg.Subject = eMailSubject;
                    msg.SubjectEncoding = System.Text.Encoding.UTF8;
                    msg.Body = string.Format(string.Format("<p>{0}</p><p>{1}</p>", eMailBodyChinese, eMailBodyEnglish), validateCode); ;
                    msg.BodyEncoding = System.Text.Encoding.UTF8;
                    msg.IsBodyHtml = true;

                    SmtpClient client = new SmtpClient();
                    client.Credentials = new System.Net.NetworkCredential(eMailAccount, eMailPassword);
                    client.Port = 587;
                    client.Host = eMailSmtpClient;
                    client.EnableSsl = false;
                    client.Send(msg);
                    msg.Dispose();
                    client.Dispose();
                }
                catch (Exception ex)
                {
                    HttpRequestResult.fResultCode = HttpStatusCode.BadRequest;
                    HttpRequestResult.fResultMsg = ex.Message;
                    result = false;
                }
                return result;
            }

  • 相关阅读:
    C++学习之路(四):线程安全的单例模式
    C++学习之路(三):volatile关键字
    C++学习之路(五):复制构造函数与赋值运算符重载
    类对象作为函数参数进行值传递
    System V共享内存介绍
    关于迭代器失效
    C++学习之路(二):引用
    C++学习之路(一):const与define,结构体对齐,new/delete
    epoll内核源码分析
    Redux中间件之redux-thunk使用详解
  • 原文地址:https://www.cnblogs.com/spilledlight/p/9257116.html
Copyright © 2020-2023  润新知