0

Keeps breaking and crashing at client.Send(email); with the error above. Quadruple checked everything.

Here's my code:

private void submit_Click(object sender, RoutedEventArgs e)
{
    string from = "************@gmail.com";
    string to = "*******@sru.edu";
    string subject = "PSLM Test";
    string body = "PSLM Test";
    string server = "smtp.gmail.com";
    int port = 465;
    string username = "************";
    string password = "*******";

    SmtpClient client = new SmtpClient(server, port);
    client.Credentials = new NetworkCredential(username, password);

    MailMessage email = new MailMessage(from, to, subject, body);

    email.Attachments.Add(new Attachment(GlobalVariables.attachedFilePath));
    email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[0]));
    email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[1]));
    email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[2]));

    client.Send(email);
}

What am I doing wrong, please?

Filip Cornelissen
  • 3,682
  • 3
  • 31
  • 41
Shaheer
  • 3
  • 1
  • 5
  • 2
    What the underlying error? Check `InnerException` property of the exception you caught. – lorond Jul 20 '16 at 09:49
  • _Always_ inspect the [`InnerException` property](https://msdn.microsoft.com/en-us/library/windows/apps/system.exception.innerexception). – Uwe Keim Jul 20 '16 at 09:49
  • Im afraid this is not one we can simply point out the error in your code, it will have to do with the settings of your mail server. Have you checked for `InnerException` which may contain more detail as to the actual issue – Jamiec Jul 20 '16 at 09:49
  • Possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Igor Jul 20 '16 at 10:45

2 Answers2

4

Gmail SMTP port is 587, not 465. You also need to set the SmtpClient.EnableSsl property to true.

client.EnableSsl = true;

It is possible that you might need to set client.UseDefaultCredentials to false prior to setting the new network credentials. This is not always the case though - all I know is that when other options have been exhausted, this tends to work.

client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(username, password);

Since you are using gmail, you will need to allow less secure applications in your google account security settings. If you are using 2-factor authentication you will need to create an application-specific password as well, and use that in your code.

user1666620
  • 4,800
  • 18
  • 27
  • 1
    That did it! It worked like a charm! Thank you so much! @user1666620 My up votes won't show publicly until I have 15 Reputation, but I definitely up voted yours! – Shaheer Jul 20 '16 at 11:20
-1
    SmtpClient client= new SmtpClient(server, port);

    client.Credentials = new System.Net.NetworkCredential(username, password);
    client.UseDefaultCredentials = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = true;

    MailMessage email = new MailMessage(from, to, subject, body);

    email.Attachments.Add(new Attachment(GlobalVariables.attachedFilePath));
    email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[0]));
    email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[1]));
    email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[2]));

    client.Send(email);
  • 1
    this is a bad answer - it does not explain why the OPs code is incorrect, or point out what improvements are made. In fact it also will not work. – user1666620 Jul 20 '16 at 10:51
  • how u say this is bad answer?? – abaravindan Jul 20 '16 at 10:56
  • 1
    I've explained in my comment above. All you did was post some code with zero explanation of what changes you made or why, or how your code is better. – user1666620 Jul 20 '16 at 11:07
  • An answer with *just* code and no explanation or description of the change(s) you've made is a bad answer. – Jamiec Jul 20 '16 at 12:17