Friday, December 11, 2015

Send mail as html format with attachment and embedded picture/video using script task of SSIS package/C#.NET

Create a script task and try below code to configure html mail with a file as attachment and an embedded image/video then send it. No required SMTP SSIS connection.

Below is very much completed example, just run it with following resources. No additional references are required.

Attachment           : C:\\test.txt
Embedded Image  : C:\\gmail.jpg

public void Main()
{
                String MailCC = "MailCC@gmail.com";
                String BCCAddress = "BCCAddress@gmail.com";
                String MailFrom = "MailFrom@gmail.com";
                String MailTo = "MailTo@gmail.com";
                String SMTPServer = "smtp.gmail.com";

                String EmailMsgBody = "<html> <body> <h3>test mail <i>html</i></h3> <br> <img src=\"cid:gmail\" alt=\"gmail\" v:shapes=\"Picture_x0020_1\"> </body></html>";

                String EmailSubject = "Test Subject HTML";

                MailMessage EmailCountMsg = new MailMessage(MailFrom, MailTo.Replace(";", ","), EmailSubject, EmailMsgBody);
               
                if (MailCC != "")
                                EmailCountMsg.CC.Add(MailCC.Replace(";", ","));

                if (BCCAddress != "")
                                EmailCountMsg.Bcc.Add(BCCAddress.Replace(";", ","));

                EmailCountMsg.IsBodyHtml = true;
                EmailCountMsg.Attachments.Add(new Attachment("C:\\test.txt"));
               
                // add embedded pic
                AlternateView av = new AlternateView(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(EmailMsgBody)), System.Net.Mime.MediaTypeNames.Text.Html);

                int si = EmailMsgBody.IndexOf("cid:",StringComparison.OrdinalIgnoreCase)+4; // finding "CID:" string to know pic name
                string picName = EmailMsgBody.Substring(si, EmailMsgBody.IndexOf("\"", si, StringComparison.OrdinalIgnoreCase) - si);
               
                LinkedResource pic1 = new LinkedResource("C:\\gmail.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg);
                pic1.ContentId = picName;
               
                av.LinkedResources.Add(pic1);
                EmailCountMsg.AlternateViews.Add(av);
// end add embedded pic

                SmtpClient SMTPForCount = new SmtpClient(SMTPServer);
                SMTPForCount.Credentials = CredentialCache.DefaultNetworkCredentials;
                //SMTPForCount.Credentials = new NetworkCredential("USER", "PASSWORD", "DOMAIN");

                SMTPForCount.Send(EmailCountMsg);

                // TODO: Add your code here
                Dts.TaskResult = (int)ScriptResults.Success;

3 comments:

  1. fixed by adding the .Net and send mail name spaces

    ReplyDelete
  2. Nothing Is Bug …: Send Mail As Html Format With Attachment And Embedded Picture/Video Using Script Task Of Ssis Package/C.Net >>>>> Download Now

    >>>>> Download Full

    Nothing Is Bug …: Send Mail As Html Format With Attachment And Embedded Picture/Video Using Script Task Of Ssis Package/C.Net >>>>> Download LINK

    >>>>> Download Now

    Nothing Is Bug …: Send Mail As Html Format With Attachment And Embedded Picture/Video Using Script Task Of Ssis Package/C.Net >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete