Exemple #1
0
        /// <summary>
        /// 获得 MailMessage 对象
        /// </summary>
        private static MailMessage GetMailMessage(SmtpInformation info)
        {
            var msg = new MailMessage();

            info.ToAddresses
            .Where(addr => !addr.IsNullOrEmpty())
            .Select(addr => addr.ToLower())
            .Distinct()
            .ForEach(addr => msg.To.Add(addr));

            info.CcAddresses
            .Where(addr => !addr.IsNullOrEmpty())
            .Select(addr => addr.ToLower())
            .Distinct()
            .ForEach(addr => msg.CC.Add(addr));

            msg.From            = new MailAddress(info.FromAddress, info.FromDisplayName, Encoding.UTF8);//发件人地址(可以随便写),发件人姓名,编码
            msg.Subject         = info.Subject;
            msg.SubjectEncoding = Encoding.UTF8;
            msg.Body            = info.Body;
            msg.BodyEncoding    = Encoding.UTF8;
            msg.IsBodyHtml      = true;
            msg.Priority        = MailPriority.Normal;

            info.Attachments.ForEach(item => msg.Attachments.Add(item));

            return(msg);
        }
Exemple #2
0
        /// <summary>
        /// 异步发送
        /// </summary>
        /// <param name="info"></param>
        public static void SendMailAsync(SmtpInformation info)
        {
            var msg = GetMailMessage(info);

            var client = new SmtpClient();

            client.Host = info.Server;
            if (!string.IsNullOrEmpty(info.Username))
            {
                client.Credentials = new NetworkCredential(info.Username, info.Password);
            }
            client.SendAsync(msg, msg);
        }