• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

SMTP mail attachment (jpg) in C#

I am trying to get a screen shot of the desktop and send it as a jpg as an attachment - can some one please help
//create the mail message 
            MailMessage mail = new MailMessage();
            //set the addresses 
            mail.From = new MailAddress(frm.txtBxUser.Text + "@email.com");
#if (!DEBUG) 
            //set the content 
            mail.Subject = "user 1 is here - " + frm.strContainer;
            mail.Body = frm.txtBxUser.Text + " " + "typed a word deemed inappropriate. That word was:" + " " + statement.ToString() + " " + "on" + " " + DateTime.Now;
            //add the attachment and take the screen shot
            int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
            int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
            Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
            Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
            gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
            bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
            //string file = "test.jpg";
            Attachment file = new Attachment(file, MediaTypeNames.Application.Octet);
            //send the message 
            SmtpClient smtp = new SmtpClient("");//or
            smtp.Credentials = new System.Net.NetworkCredential("user", "pass");

Open in new window

  • 2
1 Solution
käµfm³d 👽Commented:
Which part is failing?
r3nderAuthor Commented:
I am trying to create the screenshot as test.jpg and attach it - thats seems to be the part that's failing
Éric MoreauSenior .Net ConsultantCommented:
on this line:
Attachment file = new Attachment(file, MediaTypeNames.Application.Octet);

you are not refering the filename! try:
Attachment file = new Attachment("test.jpg", MediaTypeNames.Application.Octet);
r3nderAuthor Commented:
Right on the money
Thanks emoreau!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now