Send inline image in email

前端 未结 12 1993
夕颜
夕颜 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 11:55
        protected void Page_Load(object sender, EventArgs e)
        {
            string Themessage = @"<html>
                              <body>
                                <table width=""100%"">
                                <tr>
                                    <td style=""font-style:arial; color:maroon; font-weight:bold"">
                                   Hi! <br>
                                    <img src=cid:myImageID>
                                    </td>
                                </tr>
                                </table>
                                </body>
                                </html>";
            sendHtmlEmail("from@gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
        }
    
        protected void sendHtmlEmail(string from_Email, string to_Email, string body, string           from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
        {
            //create an instance of new mail message
            MailMessage mail = new MailMessage();
    
            //set the HTML format to true
            mail.IsBodyHtml = true;
    
            //create Alrternative HTML view
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
    
            //Add Image
            LinkedResource theEmailImage = new LinkedResource("E:\\IMG_3332.jpg");
            theEmailImage.ContentId = "myImageID";
    
            //Add the Image to the Alternate view
            htmlView.LinkedResources.Add(theEmailImage);
    
            //Add view to the Email Message
            mail.AlternateViews.Add(htmlView);
    
            //set the "from email" address and specify a friendly 'from' name
            mail.From = new MailAddress(from_Email, from_Name);
    
            //set the "to" email address
            mail.To.Add(to_Email);
    
            //set the Email subject
            mail.Subject = Subject;
    
            //set the SMTP info
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail@gmail.com", "fromEmail password");
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = cred;
            //send the email
            smtp.Send(mail);
        }
    
    0 讨论(0)
  • 2020-11-22 11:57

    Try this

     string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:filename\"></body></html>";
     AlternateView avHtml = AlternateView.CreateAlternateViewFromString
        (htmlBody, null, MediaTypeNames.Text.Html);
    
     LinkedResource inline = new LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg);
     inline.ContentId = Guid.NewGuid().ToString();
     avHtml.LinkedResources.Add(inline);
    
     MailMessage mail = new MailMessage();
     mail.AlternateViews.Add(avHtml);
    
     Attachment att = new Attachment(filePath);
     att.ContentDisposition.Inline = true;
    
     mail.From = from_email;
     mail.To.Add(data.email);
     mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
     mail.Body = String.Format(
                "<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
                @"<img src=""cid:{0}"" />", att.ContentId);
    
     mail.IsBodyHtml = true;
     mail.Attachments.Add(att);
    
    0 讨论(0)
  • 2020-11-22 12:00

    An even more minimalistic example:

    var linkedResource = new LinkedResource(@"C:\Image.jpg", MediaTypeNames.Image.Jpeg);
    
    // My mail provider would not accept an email with only an image, adding hello so that the content looks less suspicious.
    var htmlBody = $"hello<img src=\"cid:{linkedResource.ContentId}\"/>";
    var alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
    alternateView.LinkedResources.Add(linkedResource);
    
    var mailMessage = new MailMessage
    {
        From = new MailAddress("youremail@host.com"),
        To = { "recipient@host.com" },
        Subject = "yourSubject",
        AlternateViews = { alternateView }
    };
    
    var smtpClient = new SmtpClient();
    smtpClient.Send(mailMessage);
    
    0 讨论(0)
  • 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 = @"<img src='cid:" + res.ContentId + @"'/>";
        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
        alternateView.LinkedResources.Add(res);
        return alternateView;
    }
    
    0 讨论(0)
  • 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 = @"<img src='cid:" + res.ContentId + @"'/>";
        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody,  
         null, MediaTypeNames.Text.Html);
        alternateView.LinkedResources.Add(res);
        return alternateView;
    }
    
    0 讨论(0)
  • 2020-11-22 12:04

    In addition to the comments above, I have the following additional comments:

    • Do not mix Attachments and AlternativeView, use one or the other. If you mix them, the inline attachments will be rendered as unknown downloads.
    • While Outlook and Google allow standard HTML-style "cid:att-001" this does NOT work on iPhone (late 2016 patch level), rather use pure alpha numeric "cid:att-001" -> "cid:att001"

    As an aside: Outlook (even Office 2015) rendering (still the clear majority for business users) requires the use of TABLE TR TD style HTML, as it does not fully support the HTML box model.

    0 讨论(0)
提交回复
热议问题