Exemplo n.º 1
0
 /// <summary>
 /// 发送HTML格式邮件
 /// </summary>
 /// <param name="config">SMTP配置</param>
 /// <param name="AddrFrom">发件人邮箱</param>
 /// <param name="AddrTo">收件人邮箱</param>
 /// <param name="Subject">主题</param>
 /// <param name="BodyContent">内容</param>
 /// <returns></returns>
 public static string MailTo(SmtpConfig config,
     MailAddress AddrFrom, MailAddress AddrTo,
     string Subject, string BodyContent)
 {
     return MailTo(config, AddrFrom, AddrTo, null, null, Subject, BodyContent, true, null);
 }
Exemplo n.º 2
0
        /// <summary>
        /// 发送HTML格式邮件(UTF8)
        /// </summary>
        public static string MailTo(SmtpConfig config,
            MailAddress AddrFrom, MailAddress AddrTo,
            MailAddressCollection cc, MailAddressCollection bCC,
            string Subject, string BodyContent, bool isHtml, List<Attachment> attC)
        {
            MailMessage msg = new MailMessage(AddrFrom, AddrTo);

            #region 抄送
            if (cc != null && cc.Count > 0)
            {
                foreach (MailAddress cAddr in cc)
                {
                    msg.CC.Add(cAddr);
                }
            }
            #endregion

            #region 密送
            if (bCC != null && bCC.Count > 0)
            {
                foreach (MailAddress cAddr in bCC)
                {
                    msg.Bcc.Add(cAddr);
                }
            }
            #endregion

            #region 附件列表
            if (attC != null && attC.Count > 0)
            {
                foreach (Attachment item in attC)
                {
                    msg.Attachments.Add(item);
                }
            }
            #endregion

            msg.IsBodyHtml = isHtml;
            msg.Priority = MailPriority.High;

            msg.Subject = Subject;
            msg.SubjectEncoding = config.ContentEncoding;
            msg.BodyEncoding = config.ContentEncoding;
            msg.Body = BodyContent;

            SmtpClient client = new SmtpClient(config.SmtpServer, config.Port);
            if (config.Credentials != null)
            {
                client.Credentials = config.Credentials;
            }
            client.EnableSsl = config.SSLConnect;

            try
            {
                client.Send(msg);
                return "0";
            }
            catch (Exception exp)
            {
                return exp.Message;
            }
        }