示例#1
0
        public void CreateMail(ConfigMail mail)
        {
            Mail      = new MailMessage();
            Mail.From = new MailAddress(mail.From);

            if (mail.To != null)
            {
                foreach (var t in mail.To)
                {
                    Mail.To.Add(t);
                }
            }
            if (mail.CC != null)
            {
                foreach (var t in mail.CC)
                {
                    Mail.CC.Add(t);
                }
            }

            Mail.Subject      = mail.Subject;
            Mail.Body         = mail.Body;
            Mail.IsBodyHtml   = true;
            Mail.BodyEncoding = System.Text.Encoding.UTF8;
        }
示例#2
0
        public void CreateMultiMail(ConfigMail mail)
        {
            CreateMail(mail);

            Mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("如果你看到此信息,说明你的邮箱客户端不支持html.", Encoding.UTF8, "text/plain"));

            List <LinkedResource> resList = new List <LinkedResource>();

            if (mail.Resources != null)
            {
                int index = 0;
                foreach (string resource in mail.Resources)
                {
                    if (string.IsNullOrEmpty(resource) || !File.Exists(resource))
                    {
                        continue;
                    }

                    var image = new LinkedResource(resource, "image/jpeg");
                    image.ContentId = "pic" + index;
                    mail.Body      += "<br><img src=\"cid:" + image.ContentId + "\">";
                    resList.Add(image);
                }
            }
            var html = AlternateView.CreateAlternateViewFromString(mail.Body, Encoding.UTF8, "text/html");

            foreach (LinkedResource resource in resList)
            {
                html.LinkedResources.Add(resource);
            }

            Mail.AlternateViews.Add(html);

            if (mail.Attachments != null)
            {
                foreach (var attachment in mail.Attachments)
                {
                    Mail.Attachments.Add(new Attachment(attachment));
                }
            }
        }