1

I'm trying to write a code, what would save the content of a picturebox ( works ) and email it ( doesn't work ).

What do you think might be the problem? Should there be anything more to the SmtpClient client = new SmtpClient("smtp.gmail.com"); ?

Also the program shouldn't freeze up while the image gets uploaded, rather then, if necessary, be able to simultaneously upload a few images.

            System.Drawing.Image img = pictureBox1.Image;
            string name = "" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".jpg";
            img.Save(name, System.Drawing.Imaging.ImageFormat.Jpeg);

            if (chb_notif.Checked == true) ////////////// SEND EMAIL!
            {

                MailMessage message = new MailMessage(
                   "do-not-reply@123.com",
                   tb_email.Text ,
                   "VIDEO FENCE",
                   "Your perimeter has been breeched! System name: " + Environment.MachineName + "." );

                Attachment data = new Attachment(name);

                ContentDisposition disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(name);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(name);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(name);

                message.Attachments.Add(data);

                //Send the message.
                SmtpClient client = new SmtpClient("smtp.gmail.com");

                client.Credentials = CredentialCache.DefaultNetworkCredentials;

                client.Send(message);
            }

Thanks!

Roger
  • 6,443
  • 20
  • 61
  • 88

2 Answers2

1

If you dont want your app to hang while this may take some time (if the image is big, or the servers are unresponsive, you need to put it into a separate thread. (Many examples already exist)

As a few of us have also pointed out, you need to also send the Email, your code above doesnt do that. Be aware of course that if gmail thinks you're trying to relay through them, the mail probably wont send.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • Just added the client.Send(message); , but still doesn't do anything. If gmail doesn't like its SMTP being used that way, maybe there are some SMTPs that won't mind? Or should I somehow do this through my gmail account? – Roger Jun 05 '11 at 15:55
  • when you say doesnt do anything, how do you mean? – BugFinder Jun 05 '11 at 15:58
  • I mean that it doesn't send the email. :) – Roger Jun 05 '11 at 15:59
  • or do you mean the receiver doesnt receive? – BugFinder Jun 05 '11 at 16:01
  • yes. and now it also says "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required." – Roger Jun 05 '11 at 16:06
  • seems from above you had forgotten to set SSL, So there you go, once you forfilled the gmail requirements your code worked, well done – BugFinder Jun 05 '11 at 16:59
1

for:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required"

Try use:

 var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential("username", "password"),
            EnableSsl =true
        };

        client.Send(message);
danyolgiax
  • 12,798
  • 10
  • 65
  • 116