! . . Album Hình . . !

Tìm kiếm Những Gì Bạn Thích !

13 tháng 4, 2019

C# send mail with image attachment and embeded inline

private MailMessage makeupDataForSendMail(string htmlBody, MailMessage mail)
        {
            string regexImgSrc = @"]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
            MatchCollection matchesImgSrc = Regex.Matches(htmlBody, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            foreach (Match m in matchesImgSrc)
            {
                string src_url = m.Groups[1].Value;
                string pathOfFile = System.Web.HttpContext.Current.Server.MapPath(HttpUtility.UrlDecode(src_url));
                var splitPaths = pathOfFile.Split('.');
                string extention_file = splitPaths[splitPaths.Length - 1].ToLower();
                LinkedResource inline = null;
                switch (extention_file)
                {
                    case "gif":
                        {
                            inline = new LinkedResource(pathOfFile, MediaTypeNames.Image.Gif);
                            break;
                        }
                    case "png":
                        {
                            inline = new LinkedResource(pathOfFile, "image/png");
                            break;
                        }
                    default:
                        inline = new LinkedResource(pathOfFile, MediaTypeNames.Image.Jpeg);
                        break;
                }
                inline.ContentId = Guid.NewGuid().ToString();
                htmlBody = htmlBody.Replace(src_url, "cid:" + inline.ContentId);
                AlternateView avHtml = AlternateView.CreateAlternateViewFromString
               (htmlBody, null, MediaTypeNames.Text.Html);
                avHtml.LinkedResources.Add(inline);
                mail.AlternateViews.Add(avHtml);

                Attachment att = new Attachment(pathOfFile);
                att.ContentDisposition.Inline = true;
                mail.Attachments.Add(att);
            }

            return mail;
        }

        public void sendMail(string htmlBody, string toMail)
        {
            SmtpClient client = new SmtpClient();
            client.Port = int.Parse(this.GetValue("SmtpPort", listVariables));
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("username", "passwrd");
            client.Host = this.GetValue("SmtpServer", listVariables);

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("frommail");
            mail.To.Add(toMail);
            mail.Subject = "subject title";
            mail.IsBodyHtml = true;
            mail = makeupDataForSendMail(htmlBody, mail);
            client.Send(mail);
        }

Now you can call: sendMail(, "tomail@com" )

Hướng dẫn đăng nhận xét của bạn

  • Nếu muốn đăng nhận xét của mình các bạn click vào "Xem và nhận xét ở đây" dưới mỗi bài đăng, sau đó hộp thoại xuất hiện bạn gõ vào những nhận xét của mình. thế là xong! cảm ơn các bạn đã ghé thăm blog của mình !