当前位置:编程学习 > C#/ASP.NET >>

发送电子邮件

public class EmailNotificationService
    {
        /// <summary>

        /// 构造一个邮件通知服务类的实例。

        /// </summary>

        /// <param name="smtpService">SMTP服务器的IP地址</param>

        /// <param name="enableSSL">是否使用SSL连接SMTP服务器器</param>

        /// <param name="port">SMTP服务器端口</param>

        /// <param name="loginName">用于登录SMTP服务器的用户名</param>

        /// <param name="password">登录密码</param>
        private readonly string m_smtpService;
        private readonly string m_loginName;
        private readonly string m_password;
        private readonly bool m_enableSSL;
        private readonly int m_port;
        public EmailNotificationService(string smtpService,bool enableSSL,int port,string loginName,string password)
        {
            this.m_smtpService = smtpService;
            this.m_password = password;
            this.m_loginName = loginName;
            this.m_enableSSL = enableSSL;
            this.m_port = port;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sendName"></param>
        /// <param name="address"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public void SendTo(string sendName,string address,string title,string content) 
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(address);
            mail.From = new MailAddress(this.m_loginName, sendName, Encoding.UTF8);
            mail.Subject = title;
            mail.Body = content;
            mail.BodyEncoding = Encoding.UTF8;
            mail.IsBodyHtml = false;
            mail.Priority = MailPriority.Normal;

            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new NetworkCredential(this.m_loginName, this.m_password);
            smtp.Host = this.m_smtpService;
            smtp.EnableSsl = this.m_enableSSL;
            smtp.Port = this.m_port;
            try
            {
                smtp.Send(mail);
            }
            catch (SmtpFailedRecipientsException ex)
            {
                for (int i = 0; i < ex.InnerExceptions.Length; i++)
                {
                    SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
                    if (status == SmtpStatusCode.MailboxBusy ||
                        status == SmtpStatusCode.MailboxUnavailable)
                    {
                        Console.WriteLine("Delivery failed - retrying in 5 seconds.");
                        System.Threading.Thread.Sleep(5000);
                        smtp.Send(mail);
                    }
                    else
                    {
                        Console.WriteLine("Failed to deliver message to {0}",
                            ex.InnerExceptions[i].FailedRecipient);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in RetryIfBusy(): {0}",
                        ex.ToString());
            }

        }

老是报错  + InnerException {"由于目标计算机积极拒绝,无法连接。 220.181.12.18:587"} System.Exception {System.Net.Sockets.SocketException}


请问是什么原因 --------------------编程问答-------------------- 换其他邮箱
服务器是否屏蔽 --------------------编程问答-------------------- 端口号有没有错?
要不就是那边的拦截了
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,