0

I made a little application that captures the screen using this code:

Bitmap b = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
Graphics g = Graphics.FromImage(b);
g.CopyFromScreen(Point.Empty,Point.Empty,Screen.PrimaryScreen.WorkingArea.Size);

Now i want to attach the screenshot to a mail. I already wrote the neccesary code for sending the mail and all I want is to attach the image. Here is the code that I used for the mail:

MailMessage message = new MailMessage();
        message.From = new MailAddress("mail_address@gmail.com");
        message.Subject = "Subject";
        message.Body = "Body";
        message.To.Add("mail_address@gmail.com");
SmtpClient client = new SmtpClient();
        client.Credentials = new NetworkCredential("mail_address@gmail.com", "password");
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.Send(message);

Can you help me with this? Thank you.

Erik Philips
  • 48,663
  • 7
  • 112
  • 142
Joker
  • 251
  • 1
  • 3
  • 6
  • Please have a look at this question, http://stackoverflow.com/q/2825950/1443529. You could use `Attachment` with constructor accepting `Attachment(Stream, ContentType)` – cpz Aug 18 '13 at 00:21
  • 1
    Yes for inline attachments you can have a look at @JeremyThompson 's reference. – cpz Aug 18 '13 at 00:25

1 Answers1

0

Check answer given below.You need to attach image as an attachment. Hope it helps.

    MailMessage message = new MailMessage();
    message.From = new MailAddress("mail_address@gmail.com");
    message.Subject = "Subject";
    message.Body = "Body";
    message.To.Add("mail_address@gmail.com");
    string filepath = "C:\image.jpg";     // Image File Path 
    mail.Attachments.Add(new Attachment(filepath)); 

    SmtpClient client = new SmtpClient();
    client.Credentials = new NetworkCredential("mail_address@gmail.com", "password");
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.Send(message);
Muhammad Umar
  • 3,701
  • 1
  • 20
  • 32
  • I know how to attach an image using its path, but this is different. I want to attach and send the image without saving it on the computer. I want to attach it directly from the variable "b". Anyway, I found the solution and I will post it here. – Joker Aug 18 '13 at 15:19
  • Here is the soultion that I found: http://stackoverflow.com/questions/7529968/sending-screenshot-via-c-sharp . It is exactly what I asked. – Joker Aug 18 '13 at 15:24