Solved

SMTP mail attachment (jpg) in C#

Posted on 2011-03-04
4
799 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 75

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

20 Experts available now in Live!

Get 1:1 Help Now