Send inline image in email

前端 未结 12 2204
夕颜
夕颜 2020-11-22 11:09

Having an issue sending an image via email as an embedded image in the body. The image file shows as an attachment which is ok but the inline image portion just shows as a r

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 12:02

    Some minimal c# code to embed an image, can be:

    MailMessage mailWithImg = getMailWithImg();
    MySMTPClient.Send(mailWithImg); //* Set up your SMTPClient before!
    
    private MailMessage getMailWithImg() {
        MailMessage mail = new MailMessage();
        mail.IsBodyHtml = true;
        mail.AlternateViews.Add(getEmbeddedImage("c:/image.png"));
        mail.From = new MailAddress("yourAddress@yourDomain");
        mail.To.Add("recipient@hisDomain");
        mail.Subject = "yourSubject";
        return mail;
    }
    private AlternateView getEmbeddedImage(String filePath) {
        LinkedResource res = new LinkedResource(filePath);
        res.ContentId = Guid.NewGuid().ToString();
        string htmlBody = @"";
        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
        alternateView.LinkedResources.Add(res);
        return alternateView;
    }
    

提交回复
热议问题