Solved

SMTP mail attachment (jpg) in C#

Posted on 2011-03-04
4
793 Views
Last Modified: 2013-11-30
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) 
            mail.To.Add("email@email.com");
#endif
#if DEBUG
            mail.To.Add("email@email.com");
#endif
            mail.Bcc.Add("email@email.com");
            //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);
            mail.Attachments.Add(file);
            //send the message 
            SmtpClient smtp = new SmtpClient("00.00.0.00");//or 00.00.0.00
            smtp.Credentials = new System.Net.NetworkCredential("user", "pass");
            smtp.Send(mail);

Open in new window

0
Comment
Question by:r3nder
  • 2
4 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35040120
Which part is failing?
0
 
LVL 6

Author Comment

by:r3nder
ID: 35040151
I am trying to create the screenshot as test.jpg and attach it - thats seems to be the part that's failing
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 35040567
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);
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 35059122
Right on the money
Thanks emoreau!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now