Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am unable to view the Image on sending mail via asp.net with c# 2.0/c# 1.1. My code as follows:
C#
string strMailContent = "Welcome new user";
string fromAddress = "alexander.junior@nestgroup.net";
string toAddress = "alexander.junior@nestgroup.net";
string contentId = "image1";
string path = Server.MapPath(@"Images/welfare.jpg"); // my logo is placed in images folder
MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
mailMessage.Bcc.Add("alexander.junior@nestgroup.net"); // put your id here
mailMessage.Subject = "Welcome new User";

LinkedResource logo = new LinkedResource(path);
logo.ContentId = "companylogo";
// done HTML formatting in the next line to display my logo
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(logo);

mailMessage.AlternateViews.Add(av1);
mailMessage.IsBodyHtml = true;
SmtpClient mailSender = new SmtpClient("mail.nestgroup.net"); //use this if you are in the development server
mailSender.Send(mailMessage);
Posted
Updated 5-Aug-14 19:37pm
v3

If you want to embedded in to body use

C#
try
            {
                // Add image attachment from local disk
                Attachment oAttachment = oMail.AddAttachment( "d:\\test.gif" );

                // Specifies the attachment as an embedded image
                // contentid can be any string.
                string contentID = "test001@host";
                oAttachment.ContentID = contentID;
                oMail.HtmlBody = "<html><body>this is a <img src="\"cid:"<br" mode="hold" />                     + contentID + "\"> embedded image.</body></html>";

                Console.WriteLine("start to send email with embedded image...");
                oSmtp.SendMail(oServer, oMail);
                Console.WriteLine("email was sent successfully!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to send email with the following error:");
                Console.WriteLine(ep.Message);
            } 
 
Share this answer
 
Use full url for image src

C#
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src="http://www.yourdomain.com/images/companylogo.jpg" /><br></br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);
 
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