C# .NET - Send mail with atttached image - Asked By Tamer Hatoum on 14-Nov-10 06:11 AM

Dear All;
I have an List<image> realimages ;
and I want to send email by  the images I have ..
I have wrote a code but it I can attach a file , but I cannot stream the image into bytes then attach it  by the mail..
here is my function
<pre> public  void button3_Click(object sender, EventArgs e)
 {
   MailMessage message = new MailMessage(
      "from@gmail.com",
      "to@hotmail.com",
      "Quarterly data report.",
      "See the attached spreadsheet.");
  
   MemoryStream stream = new MemoryStream();
   realimage[0].Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
    // MemoryStream ms = new MemoryStream();
    // StreamWriter sw = new StreamWriter(stream);
   //sw.Write();
    // sw.Flush();
   stream.ToArray();
   Attachment data = null;
   MessageBox.Show(stream.Length.ToString());
   byte[] contentAsBytes;
   // FileData holds byte[] that is the contents of the file
   contentAsBytes = stream.ToArray();
   stream.Write(contentAsBytes, 0, contentAsBytes.Length);
   stream.Seek(0, SeekOrigin.Begin);
   // content type for file info
   ContentType contentType = new ContentType();
   contentType.MediaType = MediaTypeNames.Application.Octet;
   contentType.Name = "test";
  
   ContentType contenttype = new ContentType();
   contenttype.MediaType = MediaTypeNames.Text.Html;
   // Create  the file attachment for this e-mail message.
    data = new Attachment(stream, contenttype);
      message.Attachments.Add(data);
  
   //Send the message.
   SmtpClient client = new SmtpClient("smtp.gmail.com");
   // Add credentials if the SMTP server requires them.
   client.Credentials = CredentialCache.DefaultNetworkCredentials;
   try
   {
     client.Send(message);
   }
   catch (Exception ex)
   {
     Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
         ex.ToString());
   }
   // Display the values in the ContentDisposition for the attachment.
   ContentDisposition cd = data.ContentDisposition;
   Console.WriteLine("Content disposition");
   Console.WriteLine(cd.ToString());
   Console.WriteLine("File {0}", cd.FileName);
   Console.WriteLine("Size {0}", cd.Size);
   Console.WriteLine("Creation {0}", cd.CreationDate);
   Console.WriteLine("Modification {0}", cd.ModificationDate);
   Console.WriteLine("Read {0}", cd.ReadDate);
   Console.WriteLine("Inline {0}", cd.Inline);
   Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
   foreach (DictionaryEntry d in cd.Parameters)
   {
     Console.WriteLine("{0} = {1}", d.Key, d.Value);
   }
   data.Dispose();
 }</pre>

How can i send an email by buffering the images without a file path and attach in the mail..
regards...

Anoop S replied to Tamer Hatoum on 14-Nov-10 02:20 PM
  1. Create a LinkedResource object. The LinkedResource will actually contain the binary data of the Image. This binary data is encoded as part of the email, and sent along as part of the MailMessage.

  2. Give the LinkedResource a unique name, also known as a Content-Id.

  3. Create a HTML AlternateView.

  4. Inside that HTML text, you need to use the standard <img> tag.

  5. For the "src" value, you need to point it at the Content-Id of the LinkedResource image. This is done by using the syntax <img src="cid:whatever"> The "src=cid:" part is required for the email client to recognize the <img> tag as an embedded image, while the "whatever" part is the actual Content-Id of the LinkedResource image.  This will instruct the mail client to find an embedded image named "whatever" and display the contents *without* making a http:// request.

static void EmbedImages()
{
//create the mail message
MailMessage mail = new MailMessage();
 
//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");
 
//set the content
mail.Subject = "This is an email";
 
//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
 
//then we create the Html part
//to embed images, we need to use the prefix 'cid' in the img src value
//the cid value will map to the Content-Id of a Linked resource.
//thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
 
//create the LinkedResource (embedded image)
LinkedResource logo = new LinkedResource( "c:\\temp\\logo.gif" );
logo.ContentId = "companylogo";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);
 
//add the views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
 
 
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);
}