1

i tried send email according the code:

http://msdn.microsoft.com/es-es/library/system.net.mail.smtpclient(v=vs.110).aspx

Sending email in .NET through Gmail

so i did:

SmtpClient client = new SmtpClient("smtp.gmail.com",587);
MailAddress from = new MailAddress("xxx@gmail.com","Jane " + (char)0xD8 + " Clayton",System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("yyy@gmail.com");
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userState = "test message1";
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
if (answer.StartsWith("c") && mailSent == false)
{
    client.SendAsyncCancel();
}
message.Dispose();
Console.WriteLine("Goodbye.");
return View();

but occur problem in line "client.SendAsync(message, userState);" and error said:

An unhandled exception of type 'System.Net.Mail.SmtpException' in System.dll but was not handled in user code.

Additional Information: Error when sending mail.

how can i fix it??

Community
  • 1
  • 1
username
  • 11
  • 5
  • 1
    `Console.ReadLine()` in an ASP.NET project? Which console does it read from? – John Saunders Aug 07 '14 at 00:37
  • well... i changed code to [link](http://pastie.org/9454367#) but didnt work.... and error still here "Exception is: System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote ---> System.Net.Sockets.SocketException server: An error occurred during the connection attempt because the connected party did not properly respond after a period time or an error occurred in the connection established because connected host has failed to respond...." – username Aug 07 '14 at 23:45
  • You should post that entire exception in your question. It makes a big difference. The exception is pretty clear: the system you're trying to talk to isn't answering. The reasons are the same as for a phone call: wrong number, right number but they didn't pick up, right number but they're busy. Next step is to figure out which one is true. – John Saunders Aug 07 '14 at 23:53
  • thanks for responde and explanation!!...m... so i need change to another port?? like "25"?? but occur the same error, the error entire: [link](http://pastie.org/9454402) where line 105 is "smtp.Send(message);" – username Aug 08 '14 at 00:02
  • You need to change to the _correct_ port, and you need to make sure you can reach that port from your machine. Like @SLaks says, your machine is probably preventing outgoing connections, at least to smtp.gmail.com. – John Saunders Aug 08 '14 at 01:33
  • those ports are 587, 465 or 25. According the page:[google smtp](https://support.google.com/mail/answer/78775?hl=en), and i have tried all ports... but didnt work. – username Aug 08 '14 at 22:46

3 Answers3

1

Gmail requires SSL connections.

Set EnableSsl to true.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • so i tried: [link](http://pastie.org/9454367) but didnt work too... error said:Exception is: System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote ---> System.Net.Sockets.SocketException server: An error occurred during the connection attempt because the connected party did not properly respond after a period time or an error occurred in the connection established because connected host has failed to respond............etc. – username Aug 07 '14 at 23:39
  • Your server is probably blocking outbound connections. – SLaks Aug 08 '14 at 00:05
  • what does that means?? M... What can i do for resolve it?? Sorry... im ignorance for knowledge of Net... – username Aug 08 '14 at 22:43
  • Ask your host to enable outbound connections on that port, or switch to a better host. – SLaks Aug 08 '14 at 22:46
0

Keep your code inside the try catch block first -

.....//other codes

try{

    client.SendAsync(message, userState);

}
catch(Exception e){
    //add a breakpoint here to see what is the error
}

Then, add a break point inside the catch block and start debugging to see what is the error/exception. Usually most of the exception contains instructions to fix it. So you find the error, you find the solution.

brainless coder
  • 5,932
  • 1
  • 14
  • 34
  • error said:Exception is: System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote ---> System.Net.Sockets.SocketException server: An error occurred during the connection attempt because the connected party did not properly respond after a period time or an error occurred in the connection established because connected host has failed to respond............etc. – username Aug 07 '14 at 23:43
0

finally i changed my code something like>>

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("RecipientMail");
            msg.From = new MailAddress("sender@gmail.com", "gmailpassword", System.Text.Encoding.UTF8);
            msg.Subject = "UR SUBJECT";
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = "UR BODY";
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = false;

            //Aquí es donde se hace lo especial
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "gmailpassword");
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true; //Esto es para que vaya a través de SSL que es obligatorio con GMail
            try
            {
                client.Send(msg);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }

and setting my gmail Account in this page>> https://www.google.com/settings/security/lesssecureapps

Thank you for all your helps!!

username
  • 11
  • 5
  • Which console do you expect the exception to be output to? If this is ASP.NET, then you should just get rid of the try/catch block. The full exception will be displayed on the "yellow screen of death". – John Saunders Sep 08 '14 at 18:44
  • Also, both `MailMessage` and `SmtpClient` implement the `IDisposable` interface, so you should have `using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage()){ ... using (SmtpClient client = new SmtpClient()) { ... }}` – John Saunders Sep 08 '14 at 18:45