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:

c# httpclient The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

 If we get this error while trying to get http reponse using HttpClient object, it could mean that certificate validation fails for the remo...