Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey i tried with this concept, text "hello world" coming but my image not coming whtever am setting in code only small imagebox coming..what is the reason?? Can anyone help to resolve this issue...?

What I have tried:

for this i am using html like this
HTML
<html>
<body>
    <table border="0" cellpadding="0" cellspacing="0" width="100%" height="50%><br" mode="hold" />
	<tr>
            <td width="230" valign="top">
                <table border="0" width="50%">
                    <tr>
                        <td>
                            Dear All,
                            
                             a very Happy Birthday!!
                        </td>
                    </tr>
                    <tr style="...">
                        <td style="font-weight:bold;">
                            Hello World!<br>
                            <div style="...">
                                <img src=cid:myImageID style="position: relative; top: 0; left: 0;" />
                            </div>
                        </br></td>
                    </tr>
                </table>
            </td>
        </tr>
</body>
</html>

C#
//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(imagePath);
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("username", "pwd");
SmtpClient smtp = new SmtpClient("server", port);
//smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
//send the email
smtp.Send(mail);

Please tell me where i am doing wrong??
Posted
Updated 12-Feb-16 6:33am
v2
Comments
Sinisa Hajnal 12-Feb-16 10:00am    
Your body does not contain image element. You have to reference it in the body for it to be shown. Just adding it to the message is not enough. As for exact syntax, google :)
Richard Deeming 12-Feb-16 12:33pm    
I've fixed your code formatting, which was breaking the rest of the page.

1 solution

"Embedded image" is a part of a multipart e-mail with separate content-type and disposition. Technically, such part is created using the class System.Net.Mail.Attachment:
Attachment Class (System.Net.Mail)[^].

On content types (AttachmentBase.ContentType Property (System.Net.Mail)[^]), see also my past answer: How to attach a file in smtp mail implementation[^].

Normally, you place your image part in the form of base64-encoded string. Please see:

The attachment will be presented in a mail viewer as embedded in your message, if you chose content disposition properly. I explain it in my past answer: Dynamically generate pdf file and Send an Email as a attachment.[^].

In the answer referenced above, you will find all links you need to embed your images.

If you also need to create a hyperlink to the image inside a mail message, you would need to use the special URI scheme "cid" (Content-id). Please see my past answer: Sending HTML attachment with images[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900