1

I am trying to send mail through gmail. I am sending mail successfully when I am testing on localhost, but this does not work when I upload online. Below is my code used to send email. Any help please ?

MailMessage mm = new MailMessage();
            mm.From = new MailAddress("donotreply@gmail.com");
            mm.Subject = "Test";
            mm.To.Add(new MailAddress(email));
            mm.Body = "Test";
            mm.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential("donotreply@gmail.com", "password");
            client.Send(mm);
Mark Fenech
  • 1,308
  • 6
  • 26
  • 36
  • 1
    Where online are you uploading your files? It could be that the firewall is blocking that specific port. (587) – Jethro Oct 19 '13 at 11:48
  • 1
    Aren't there any kind of log you can get from the server ? Like the security log. – navit Oct 19 '13 at 11:52
  • Does not work isn't a good error description. Look at the error it shoud contain a hint of what the problem is. A guess would be that the smtp service simply isn't running. – Ralf Oct 19 '13 at 12:05
  • I don't know the error as I have an error page and if I remove that I will still get something on web config custom errors – Mark Fenech Oct 19 '13 at 19:27

2 Answers2

0

Your question looks like it may be answered by this similar StackOverflow question:

Sending email in .NET through Gmail

Let me know if that does not answer all of your questions.

Community
  • 1
  • 1
Jason Enochs
  • 1,350
  • 1
  • 11
  • 19
0

In your current code above, you first set:

client.UseDefaultCredentials = true

which tells the framework to use the default credentials, and then you go on to define your custom credentials:

 client.Credentials = new System.Net.NetworkCredential("donotreply@gmail.com", "password");

Which it will not use when it tries to send mail, because you've already told it to use the default credentials two lines above. Try setting

client.UseDefaultCredentials = false
Ben
  • 881
  • 6
  • 17
  • You may want to elaborate on how this code would answer the question. As it is now, it has been automatically flagged as low quality. – Heretic Monkey Mar 24 '15 at 15:42