Exemplo n.º 1
0
        /// <summary>
        /// SMTP邮件Sender的类的封装
        /// </summary>
        /// <param name="smtpServer">smtp服务地址</param>
        /// <param name="port">端口号</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="enableSSL">是否采用ssl的方式</param>
        public override bool Send(string smtpServer, int port, string userName, string password, bool enableSSL)
        {
            bool isSended = false;

            try
            {
                MailMessage message = new MailMessage();
                To.ForEach(email =>
                {
                    if (email.IsValidEmail())
                    {
                        message.To.Add(email);
                    }
                });
                message.Sender = new MailAddress(userName, userName);
                if (CC != null && CC.Any())
                {
                    CC.ForEach(email =>
                    {
                        if (email.IsValidEmail())
                        {
                            message.CC.Add(email);
                        }
                    });
                }
                message.Subject = Encoding.Default.GetString(Encoding.Default.GetBytes(Subject));

                message.From = DisplayName.IsBlank() ? new MailAddress(From) : new MailAddress(From, Encoding.Default.GetString(Encoding.Default.GetBytes(DisplayName)));

                message.Body                        = Encoding.Default.GetString(Encoding.Default.GetBytes(Body));
                message.BodyEncoding                = Encoding.GetEncoding("GBK");
                message.HeadersEncoding             = Encoding.UTF8;
                message.SubjectEncoding             = Encoding.UTF8;
                message.IsBodyHtml                  = true;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                message.ReplyToList.Add(new MailAddress(From));

                SmtpClient smtp = new SmtpClient(smtpServer, port)
                {
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(userName, password),
                    Timeout   = 30000,
                    EnableSsl = enableSSL
                };

                smtp.SendCompleted += smtp_SendCompleted;
                smtp.Send(message);
                isSended = true;
            }
            catch (Exception ex)
            {
                Logger.Log("邮件发送失败:", ex);
            }
            return(isSended);
        }
Exemplo n.º 2
0
        /// <summary>
        /// SMTP邮件Sender的类的封装
        /// </summary>
        public override async Task <bool> Send()
        {
            var isSended = false;

            try
            {
                //qq域名邮箱的smtp(smtp.qq.com);如果是腾讯企业邮箱,需要改成smtp.exmail.qq.com
                var smtpServer = "smtp.qq.com";
                var port       = 25;              //端口号
                var userName   = "******"; //用户名
                var password   = "******";        //密码

                var message = new MailMessage();
                To.ForEach(email =>
                {
                    if (email.IsEmail())
                    {
                        message.To.Add(email);
                    }
                });
                message.Sender = new MailAddress(userName, userName);
                if (Cc.AnyOne())
                {
                    Cc.ForEach(email =>
                    {
                        if (email.IsEmail())
                        {
                            message.CC.Add(email);
                        }
                    });
                }
                message.Subject = Encoding.Default.GetString(Encoding.Default.GetBytes(Subject));
                if (From.IsBlank())
                {
                    From = userName;
                }
                message.From = DisplayName.IsBlank() ? new MailAddress(From) : new MailAddress(From, Encoding.Default.GetString(Encoding.Default.GetBytes(DisplayName)));

                message.Body                        = Encoding.Default.GetString(Encoding.Default.GetBytes(Body));
                message.BodyEncoding                = Encoding.GetEncoding("GBK");
                message.HeadersEncoding             = Encoding.UTF8;
                message.SubjectEncoding             = Encoding.UTF8;
                message.IsBodyHtml                  = true;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                message.ReplyToList.Add(new MailAddress(From));
                if (Attachments.AnyOne())
                {
                    Attachments.ForEach(x =>
                    {
                        message.Attachments.Add(new Attachment(x));
                    });
                }

                var smtp = new SmtpClient(smtpServer, port)
                {
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(userName, password),
                    Timeout   = 30000,
                    EnableSsl = true
                };

                smtp.SendCompleted += smtp_SendCompleted;
                await smtp.SendMailAsync(message);

                isSended = true;
            }
            catch (Exception ex)
            {
                Log.Error("发送邮件失败", ex);
            }
            return(isSended);
        }