Sending Multiple Images in an Email !!!!!

Last post 12-08-2009 5:19 AM by suresh.o. 7 replies.

Sort Posts:

  • Sending Multiple Images in an Email !!!!!

    09-03-2009, 10:19 AM
    • Member
      36 point Member
    • rickjackson
    • Member since 03-12-2008, 11:17 AM
    • Mumbai
    • Posts 59

    Hello,

    Any 1 can tell me how to send multiple images in an email.

     

    i saw an sample application which upload the imge and send the same in an email. But i have multiple images which i need to get image hosting site. so how can i do that please show the code and i need the code for multiple images in an email.

     

     

    Thanks

  • Re: Sending Multiple Images in an Email !!!!!

    09-03-2009, 11:03 AM
    Answer
    • Star
      8,313 point Star
    • satalaj
    • Member since 11-28-2007, 12:41 AM
    • Pune
    • Posts 1,647

    You can do it in  way

     1. Set your email body format to Html and add below tag
        <img src = http://yourdomiain/image1.jpg > </img>
        <img src = http://yourdomiain/image2.jpg > </img>
        <img src = http://yourdomiain/image3.jpg > </img>
        <img src = http://yourdomiain/image4.jpg > </img>
         <img src = http://yourdomiain/image5.jpg > </img>
        <img src = http://yourdomiain/image6.jpg > </img>
        .. . .. .

    2.  you can send it as an attachment. Add multiple images as an attachment

    3. Embed it as BASE64 string


    &lt;a href="http://www.britblog.com/"&gt;&lt;img
    src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav///88PD9WqsYmApmZmZtZfYmdakyH5BAQUAP8ALAAAAABQAA8AAAPb
    WLrc/jDKSVe4OOvNu/9gqARDSRBHegyGMahqO4R0bQcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVr
    ApTYWy5FKM1IQe+Mp+L4rphz+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KT
    kpFvZj4ZbYeCiXaOiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxM
    AF15XEFRXgZWWdciuM8GCmdSQ84lLQfY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs=
    " alt="British Blog Directory" width="80" height="15" /&gt;&lt;/a&gt;


    you can conver binary to Base64 like this http://www.revenmerchantservices.com/post/2009/09/03/c-binary-to-string.aspx

  • Re: Sending Multiple Images in an Email !!!!!

    09-03-2009, 11:27 AM
    • Participant
      1,040 point Participant
    • junaid_arif
    • Member since 05-22-2008, 5:18 AM
    • Islamabad
    • Posts 193

    The following code creates 2 views Plain Text view mai HTML view and posts 2 images with the email


        protected void SendMail_Click(object s, EventArgs e)
        {
            System.Net.Mail.MailAddress sender = new System.Net.Mail.MailAddress("sender@mysite.com", "SENDER");
            System.Net.Mail.MailAddress recipient = new System.Net.Mail.MailAddress("recipient@mysite.com", "RECIPIENT");
            System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(sender, recipient);
    
            m.Subject = "TEST MESSAGE";
    
            // Define the plain text alternate view and add to message
            string plainTextBody = "You must use an email client that supports HTML messages";
            System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(plainTextBody, null, System.Net.Mime.MediaTypeNames.Text.Plain);
    
            // Add the Plain view
            m.AlternateViews.Add(plainTextView);
            
            // Add the HTML view
            //---------------------------------------------------------------------------
            // To embed images, we need to use the prefix 'cid' in the img src value
            string htmlBody = "<strong>This is a test mail containing two image file as attachments</strong>";
            htmlBody += "<img alt='Alternate Text1' src='cid:uniqueId1' /> <img alt='Alternate Text1' src='cid:uniqueId2' />";
            htmlBody += "<strong>End of Mail</strong>";
            System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
            // Create the image resource from image path using LinkedResource class..            
            System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource("c:\\attachment\\image1.jpg", System.Net.Mime.MediaTypeNames.Text.Html);
            imageResource.ContentId = "uniqueId1";
            imageResource.TransferEncoding =  System.Net.Mime.TransferEncoding.Base64;
            // Adding the image1 linked to htmlView...
            htmlView.LinkedResources.Add(imageResource);
    
            // Create the image resource from image path using LinkedResource class.. 
            imageResource = new System.Net.Mail.LinkedResource("c:\\attachment\\image2.jpg", "image/jpeg");
            imageResource.ContentId = "uniqueId2";
            imageResource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
            // Adding the image2 linked to htmlView...
            htmlView.LinkedResources.Add(imageResource);
            //---------------------------------------------------------------------------
            m.AlternateViews.Add(htmlView);
    
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
            {
                Host = "smtp.mysmtp.com",
                UseDefaultCredentials = false,
                Credentials = new System.Net.NetworkCredential("username", "password")
            };
    
            smtp.Send(m);
        }


    Regards,

    Junaid Arif
    Software Engineer
    Specializing In Microsoft Solutions
  • Re: Sending Multiple Images in an Email !!!!!

    09-03-2009, 12:11 PM
    • Member
      36 point Member
    • rickjackson
    • Member since 03-12-2008, 11:17 AM
    • Mumbai
    • Posts 59

     I Tried your method but the image is not showing it up cause i am using image hosting site where all the images are uploaded and the problem is image name like http://imagehosting/images/taj mahal.jpg or http://imagehosting/images/taj , mahal.jpg so how to handle such kind of seconerio

  • Re: Sending Multiple Images in an Email !!!!!

    09-03-2009, 12:21 PM
    • Participant
      1,040 point Participant
    • junaid_arif
    • Member since 05-22-2008, 5:18 AM
    • Islamabad
    • Posts 193

    You do not need to use these complex procedures of alternate views and resources then if your images are hosted on web just include the path of those in <img src"PATH HERE" >/ attribute...


    User Server.UrlEcode(PathHere) method to encode file names...

    Regards,

    Junaid Arif
    Software Engineer
    Specializing In Microsoft Solutions
  • Re: Sending Multiple Images in an Email !!!!!

    09-03-2009, 12:25 PM
    Answer
    • Participant
      1,040 point Participant
    • junaid_arif
    • Member since 05-22-2008, 5:18 AM
    • Islamabad
    • Posts 193

    try this

        protected void SendMail_Click(object s, EventArgs e)
        {
            System.Net.Mail.MailAddress sender = new System.Net.Mail.MailAddress("sender@mysite.com", "SENDER");
            System.Net.Mail.MailAddress recipient = new System.Net.Mail.MailAddress("recipient@mysite.com", "RECIPIENT");
            System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(sender, recipient);
            m.Subject = "TEST MESSAGE";
            // Define the plain text alternate view and add to message
            string plainTextBody = "You must use an email client that supports HTML messages";
            System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(plainTextBody, null, System.Net.Mime.MediaTypeNames.Text.Plain);
            // Add the Plain view
            m.AlternateViews.Add(plainTextView);
            // Add the HTML view
            //---------------------------------------------------------------------------
            // To embed images, we need to use the prefix 'cid' in the img src value
            string htmlBody = "<strong>This is a test mail containing two image file as attachments</strong>";
            htmlBody += "<img alt='Alternate Text1' src='" + Server.UrlEncode("http://imagehosting/images/taj mahal.jpg") + "' /> <img alt='Alternate Text1' src='" + Server.UrlEncode("http://imagehosting/images/taj , mahal.jpg") + "' />";
            htmlBody += "<strong>End of Mail</strong>";
            System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
            m.AlternateViews.Add(htmlView);
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
            {
                Host = "smtp.mysmtp.com",
                UseDefaultCredentials = false,
                Credentials = new System.Net.NetworkCredential("username", "password")
            };
    
            smtp.Send(m);
        }


    Regards,

    Junaid Arif
    Software Engineer
    Specializing In Microsoft Solutions
  • Re: Sending Multiple Images in an Email !!!!!

    14 minutes ago
    • Member
      3 point Member
    • kaur.jazz
    • Member since 08-26-2009, 5:48 AM
    • Posts 22

    Hi Junaid !

    With the help of your code, i m able to attach image when i send a mail,

    but i want my image as a background image.

    Can u please suggest me some solution ??


    Jazz


  • Re: Sending Multiple Images in an Email !!!!!

    5 minutes ago
    • Member
      40 point Member
    • suresh.o
    • Member since 08-10-2008, 4:06 AM
    • Posts 15


    You can achive this by using CSS background image. For that chedck below link

    http://www.w3schools.com/css/tryit.asp?filename=trycss_background-image

Page 1 of 1 (8 items)