Wednesday, September 10, 2014

Email with Embedded (Inline) image in sharepoint

In asp.net we can send an email with inline image as explained in this post
http://stackoverflow.com/questions/18358534/send-inline-image-in-email

But in sharepoint the procedure is slightly different. I have used below post to do the stuff

http://sharepoint-tamizha.blogspot.co.uk/2013/08/send-email-with-embedded-images-in-c.html


And the final code looks like


byte[] imgByte = null;
SPFile file = SPContext.Current.Web.GetFile(ImageUrl);
                                            imgByte = file.OpenBinary();

// Creating Unique ID for adding image referrence
string GUId1 = Guid.NewGuid().ToString();               

// frame the HTML with the img tag and above unique id
string htmlBody = "<html><body><div><img src=\"cid:" + GUId1 + "\"></div></body></html>";

// Create alternateview object with Mime type HTML
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
                    (htmlBody, nullMediaTypeNames.Text.Html);

//Create object for Linked Resource with the Image physical path or Image Stream
MemoryStream stream = new MemoryStream(imgByte);
                LinkedResource pic1 = new LinkedResource(stream, "image/png");

// Provide the previously created Unique ID to associate the Image with the respective img src.
pic1.ContentId = GUId1;

//Add the Linked Resource to the AlternateView
avHtml.LinkedResources.Add(pic1);

string from = SPContext.Current.Web.Site.WebApplication.OutboundMailSenderAddress;

string smtpAddress =SPContext.Current.Web.Site.WebApplication.OutboundMailServiceInstance.Server.Address;

// Assign SMTP address
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = smtpAddress;

MailMessage mailMessage = new MailMessage(from, toRecipients);
mailMessage.Subject = subject;

// Add the Alternate view with the Mail message
mailMessage.AlternateViews.Add(avHtml);
mailMessage.IsBodyHtml = true;


smtpClient.Send(mailMessage);

No comments:

SSL Error - The connection for this site is not secure

 After cloning a git repo of dot net framework website and trying to run it all I could see was this error Turns out the fix was to simply e...