Send Email có image trong content (Send an HTML email with embedded image and plain text alternate)

Trong đoạn code bên dưới bạn có thể add nhiều hình vào và trong content  các bạn đưa ID vào là được

MailMessage Mail = new MailMessage();

            Mail.From = new MailAddress("moss@xxx.com.vn");
            Mail.To.Add("cmtuan@xxx.com.vn");
            Mail.Subject = "This is Image Test.";
            Mail.Body = "This is the body of the email";
            LinkedResource LinkedImage = new LinkedResource(@"DataFiles\Birthday\logo1.jpg");
            LinkedImage.ContentId = "MyPic";
            //Added the patch for Thunderbird as suggested by Jorge
            LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);

            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
              "You should see image next to this line. <img src=cid:MyPic>",
              null, "text/html");

            htmlView.LinkedResources.Add(LinkedImage);
            Mail.AlternateViews.Add(htmlView);
            SmtpClient smtp = new SmtpClient("webmail.xxx.com.vn", 25);
            try
            {
                smtp.Send(Mail);
            }
            catch (SmtpException ex)
            {
                //Logger.LogException(ex);
            }

Các bạn nhớ đổi chổ của tên miền sang tên miền của server mail của các bạn.

Chúc các bạn thành công

Leave a Reply