Digambar Malla
Digambar Malla

Reputation: 335

Embedded image in mail body is not displayed in webbrowser control in winforms

I want to place the contents of a outlook mail body into a webbrowser control in winforms. everything is displayed if i use following code,but images in the page is not displayed. I am using outlook 2013 and VS2012.

webBrowser1.DocumentText = mail.HTMLBody;

I checked html it shows something like: It shows something like:

<img id=\"Picture_x0020_7\" src=\"cid:[email protected]\" width=\"904\" height=\"768\">

As i want to implement it in project i can't save the mail body in local as suggested here: how to get embed image from current outlook email with c#?^]

Any help deeply appreciated.

Upvotes: 1

Views: 1729

Answers (2)

Qasim Bataineh
Qasim Bataineh

Reputation: 707

Below is what you need to do:

    private void sendInlineImg() {
    MailMessage mail = new MailMessage();
    mail.IsBodyHtml = true;
    mail.AlternateViews.Add(getEmbeddeImage());
    mail.From = new MailAddress("yourAddress@yourDomain");
    mail.To.Add("recipient@hisDomain");
    mail.Subject = "yourSubject";
    //YourSMTPClient.Send(mail); //* Set your SMTPClient before!
}
private AlternateView getEmbeddeImage() {
    string yourFile = "c:/header.png";
    LinkedResource inline = new LinkedResource(yourFile);
    inline.ContentId = Guid.NewGuid().ToString();
    string htmlBody = @"<img src='cid:" + inline.ContentId + @"'/>";
    AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
    alternateView.LinkedResources.Add(inline);
    return alternateView;
}

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49453

You can find referenced embedded images stored as attachments in the mail item. You can save them on a disk or upload to any web server and then replace src attribute content of the <img/> tag in the code to a new value so the browser can display images correctly.

Or just try to save the mail item using the HTML file format. The SaveAs method of the MailItem class saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used. Note, you need to use the olHTML value for the Type parameter.

Upvotes: 2

Related Questions