Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear All;
I have an List 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
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();
}


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

 
Share this answer
 
http://code.msdn.microsoft.com/CSharpGmail[^] Maybe this will help you, scroll down to CsharpChico's comment. :thumbsup:
 
Share this answer
 
Comments
Tamer Hatoum 14-Nov-10 7:59am    
thanks for the links, but that doesnot help in my situation , this function :
mail.Attachments.Add(new Attachment(listBox1.Items[i].ToString()));
is for adding string to the attachment, not stream an image...
so do you have any link ro sample code how to stream the image and attach it?
regards..
euhiemf 14-Nov-10 9:52am    
No sorry, but msdn have quite a lot System.Net.Mail stuff. Maybe you can find something here: http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx or here: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
euhiemf 14-Nov-10 9:55am    
string zFile = @"c:\test.jpg";
if(!System.IO.File.Exists(zFile))
{
StreamReader reader = new StreamReader(zFile);
this.pictureBox1.Image = Image.FromStream(reader.BaseStream);
reader.Close();
}

this should stream the image if I'm not wrong. but I don't know how to attach it.
Tamer Hatoum 14-Nov-10 12:21pm    
thanks for your answer , I alread checked that links , and regarding the code which you provide is stream from a url and display the image into the picturebox...

but if you see my code :
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);
I already streaming the image and get the byte [] ...
reagards...
I already stream the image and get the by
You should read this link.

Click



Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
Tamer Hatoum 14-Nov-10 12:10pm    
this is not my question, I want to streem an image from picturebox, not by file link... thanks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900