Send inline image in email

前端 未结 12 2153
夕颜
夕颜 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

    I added the complete code below to display images in Gmail,Thunderbird and other email clients :

    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)
     {
        // below line was corrected to include the mediatype so it displays in all 
        // mail clients. previous solution only displays in Gmail the inline images 
        LinkedResource res = new LinkedResource(filePath, MediaTypeNames.Image.Jpeg);  
        res.ContentId = Guid.NewGuid().ToString();
        string htmlBody = @"";
        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody,  
         null, MediaTypeNames.Text.Html);
        alternateView.LinkedResources.Add(res);
        return alternateView;
    }
    

提交回复
热议问题