0

The goal is to send an email from a C# application. The code snippet below works for me, but when I run it at work (internet connection with a script configuration proxi.pac) it crashes.

Can someone help me?

string SendersAddress = "jd@gmail.com";
string ReceiversAddress = "dj@xxx.com";

const string SendersPassword = "xxxxxx";
const string subject = "Testing Gmail LOCAL";
const string body = "Hi This Is my Mail From Gmail";

try
{
  SmtpClient smtp = new SmtpClient
  {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    Credentials = new NetworkCredential(SendersAddress, SendersPassword),
    //Timeout = 3000
  };

  MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
  smtp.Send(message);
  Console.WriteLine("Message Sent Successfully");
  Console.ReadKey();
}
CodesInChaos
  • 100,017
  • 20
  • 197
  • 251
  • What does it crash with? – BugFinder Aug 20 '12 at 10:54
  • Can you get us what the exception is ? any error messages .etc. ? – Madushan Aug 20 '12 at 10:54
  • Are you swallowing the exceptions? Please print them and show us the message. – Tudor Aug 20 '12 at 10:54
  • From your company network you can reach Gmail? Could you check if Gmail and/or port 587 are allowed through the firewall, etc.? – Gerald Versluis Aug 20 '12 at 11:01
  • Is sending does not work : smtp.Send (message) => InnerException {"Could not connect to remote server"} I can not specify that a internet connection goes through a webproxi http://mabsquid.xxxx.local/proxy.pac – user1611524 Aug 20 '12 at 11:02
  • Yes I can connect to gmail for my business – user1611524 Aug 20 '12 at 11:05
  • Ah, but do you mean you can browse to mail.gmail.com, rather than download/upload mail via an email client? If so, the proxy server may not help you and as others have said, its most likely the gmail mail ports are blocked. – BugFinder Aug 20 '12 at 11:46

2 Answers2

0

Try with port number 25. Hope it works.

Robert Langdon
  • 835
  • 1
  • 10
  • 26
0

Try Port 465 Seems like port 587 is correct.

Note that some ISPs and proxy servers block SMTP data when it's not using port 25. This may be a deal-braker for you.

UPDATE Seems like your proxy server is blocking your communication.


It is interesting to note that a simple Google search cannot give a help page from Google that has the expected SMTP settings, although there are pages for POP and IMAP settings.

Madushan
  • 5,183
  • 25
  • 57
  • It does not work with the 587, the 25 and 465 Thank you for trying – user1611524 Aug 20 '12 at 12:16
  • Hmm... I just tried your code running on my machine. (Nothing changed, except for my Gmail credentials. Port 587 NOT 465) and it worked. (Port 465 doesn't seems to respond, btw.) It seems like the proxy you're using is blocking your communication, rather than an issue with your code. – Madushan Aug 20 '12 at 21:36
  • Also added the `catch` block which seems to be missing in your code. – Madushan Aug 20 '12 at 21:42