Exemplo n.º 1
0
        /// <summary>
        /// 发送方法
        /// </summary>
        /// <param name="to">接收方的email</param>
        /// <param name="title">邮件标题</param>
        /// <param name="htmlBody">邮件内容</param>
        /// <returns>是否成功</returns>
        public Boolean send(String to, String title, String htmlBody)
        {
            if (strUtil.IsNullOrEmpty(_senderName))
            {
                _senderName = _smtpUser;
            }

            MailAddress addrFrom = new MailAddress(_smtpUser, _senderName, Encoding.UTF8);
            MailAddress addrTo   = new MailAddress(to, to, Encoding.UTF8);

            using (MailMessage message = new MailMessage(addrFrom, addrTo)) {
                message.Subject         = title;
                message.SubjectEncoding = Encoding.UTF8;

                message.Body         = htmlBody;
                message.BodyEncoding = Encoding.UTF8;

                message.IsBodyHtml = _isBodyHtml;
                message.Priority   = _mailPriority;

                NetworkCredential credential = new NetworkCredential(_smtpUser, _smtpPwd);

                SmtpClient client = new SmtpClient(_smtpUrl);
                client.Credentials = credential;
                client.EnableSsl   = _enableSsl;

                try {
                    client.Send(message);
                    if (sendSuccessCallback != null)
                    {
                        sendSuccessCallback.SuccessRun();
                    }
                    return(true);
                }
                catch (SmtpException ex) {
                    String info = "send mail to " + to + " : " + title;
                    logger.Error("[" + info + "] error : " + ex.ToString());

                    logger.Error("msg.AddressTo=" + to);
                    logger.Error("msg.Subject=" + message.Subject);
                    logger.Error("msg.Body=" + message.Body);

                    return(false);
                }
            }
        }