Friday, March 22, 2013

Send embedded images via email to Outlook from SharePoint web part or Workflow

Recently I was working on a custom application and we had a requirement to send an email through SharePoint code with embedded images. Usually it is good practice to use SPUtility.SendEmail(...) for sending emails dynamically through code since we do not have to worry about the extracting SMTP Server information. SPUtility figures it out automatically for that server where code is running and sends these emails. But I did not find a way to embed images while sending email using SPUtility.

After spending some time I thought to use back the old fashion of .Net namespaces for sending these emails with embedded images.

1. How to embed one image in an Email and send an HTML formatted email to Outlook

Code:

 private void SendEmail( string SubjectLine, string fromaddress, string  toaddress)
        {
           
                 string strMailContent = "Send email with an image;
                string strSmtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;

                string fromAddress = fromaddress;
                string toAddress = toaddress;
           

                string templatePath = SPUtility.GetGenericSetupPath("TEMPLATE");
                string path = templatePath + "\\LAYOUTS\\featurefoldername\\image1.gif";
         

                MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
                mailMessage.Subject = SubjectLine;

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

                mailMessage.AlternateViews.Add(av1);
                mailMessage.IsBodyHtml = true;
                SmtpClient mailSender = new SmtpClient(strSmtpServer);
                mailSender.Send(mailMessage);
            }
        }


2. For embedding multiple images

 private void SendEmail(string SubjectLine, string fromaddress, string  toaddress)
        {
           
                 string strMailContent = "Send email with an image;
                string strSmtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;

                string fromAddress = fromaddress;
                string toAddress = toaddress;
           

                string templatePath = SPUtility.GetGenericSetupPath("TEMPLATE");
                string logopath1 = templatePath + "\\LAYOUTS\\Featurefoldername\\logo1.gif";
                string logopath2 = templatePath + "\\LAYOUTS\\Featurefoldername\\logo2.gif";
                string logopath3 = templatePath +"\\LAYOUTS\\Featurefoldername\\logo3.gif";

                MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
                mailMessage.Subject = SubjectLine;

                List<LinkedResource> resources = new List<LinkedResource>();
                LinkedResource logo1= new LinkedResource(logopath1);
                logo1.ContentId = "logo1";
                resources.Add(logo1);

                LinkedResource logo2= new LinkedResource(logopath2 );
                logo2.ContentId = "logo2";
                resources.Add(logo2);

                LinkedResource logo3= new LinkedResource(logopath3);
                logo3.ContentId = "logo3";
                resources.Add(logo3);

             

                // done HTML formatting in the next line to display my logo
                AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:logo1/><br><img src=cid:logo2/><br><img src=cid:logo3/><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);

           
                resources.ForEach(x => av1.LinkedResources.Add(x));
                mailMessage.AlternateViews.Add(av1);
                mailMessage.IsBodyHtml = true;
                SmtpClient mailSender = new SmtpClient(strSmtpServer);
                mailSender.Send(mailMessage);

            }

        }

No comments:

Post a Comment