4

Can we send email form local host using gmail smtp ? I am trying and getting error The operation has timed out.

I am trying to send email from local host from last 3 days. It works fine if I send emails from my hosting server using gmail but it is not working on localhost. I have disabled firewall anti virus but even then unlucky. Please guide me have u ever used gmail for sending emails from localhost (without any server involved)

If it is possible here is my code please guide me. Plese help me and guide me I am stucked.

thanks

 protected void btnConfirm_Click(object sender, EventArgs e)
{
    MailMessage message = new MailMessage();
    message.To.Add("me@hotmail.com");
    message.From = new MailAddress("xxxxxx@gmail.com");
    message.Subject = "New test mail";
    message.Body = "Hello test message succeed";
    message.IsBodyHtml = true;
    message.BodyEncoding = System.Text.Encoding.ASCII;
    message.Priority = System.Net.Mail.MailPriority.High;

    SmtpClient smtp = new SmtpClient();
    smtp.EnableSsl = true;
    smtp.Port = 465;        
    smtp.UseDefaultCredentials = false;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Host = "smtp.gmail.com";
    smtp.Credentials = new NetworkCredential("xxxxxx@gmail.com", "**mypassword**");
    try
    {
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
user576510
  • 5,777
  • 20
  • 81
  • 144
  • Have you tried removing the try/catch statements and running it in the debugger to see where it's getting stuck at? –  Jan 15 '11 at 06:08
  • on smtp.Send(message); but first thing is have u ever used gmail to send mail from loclhost ? – user576510 Jan 15 '11 at 06:10
  • Kevin I mean to ask is, is it possible ? – user576510 Jan 15 '11 at 06:12
  • possible duplicate of http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c and http://stackoverflow.com/questions/1326027/gmail-how-to-send-an-email-programmatically and http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail – Pauli Østerø Jan 15 '11 at 06:13
  • It is possible, I use to do it on a daily basis – Yuriy Faktorovich Jan 15 '11 at 06:21
  • Yuriy Faktorovich can u plz give a little more details. I mean were u using it on localhost with gmail and without using any other software etc ? Can u plz compare my code wiht yours is it ok ? – user576510 Jan 15 '11 at 06:45

3 Answers3

5

Yes, you can send email using gmail from localhost.

I once wrote a blogpost about how to send email using gmail.

Pasting the code snippet from my blogpost.

This is the working code, and I often use it.

/// <summary>
/// A Generic Method to send email using Gmail
/// </summary>
/// <param name="to">The To address to send the email to</param>
/// <param name="subject">The Subject of email</param>
/// <param name="body">The Body of email</param>
/// <param name="isBodyHtml">Tell whether body of email will be html of plain text</param>
/// <param name="mailPriority">Set the mail priority to low, medium or high</param>
/// <returns>Returns true if email is sent successfuly</returns>
public static Boolean SendMail(String to, String subject, String body, Boolean isBodyHtml, MailPriority mailPriority)
{
    try
    {
        // Configure mail client (may need additional
        // code for authenticated SMTP servers)
        SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

        // set the network credentials
        mailClient.Credentials = new NetworkCredential("YourGmailEmail@gmail.com", "YourGmailPassword");

        //enable ssl
        mailClient.EnableSsl = true;

        // Create the mail message (from, to, subject, body)
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress("YourGmailEmail@gmail.com");
        mailMessage.To.Add(to);

        mailMessage.Subject = subject;
        mailMessage.Body = body;
        mailMessage.IsBodyHtml = isBodyHtml;
        mailMessage.Priority = mailPriority;

        // send the mail
        mailClient.Send(mailMessage);

        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}
Zain Shaikh
  • 6,013
  • 6
  • 41
  • 66
2

If the error is Operation has timed out, then one possibility is that network firewall is blocking outgoing access to the specified host/port. This would be the case in offices which have firewall/proxy servers to restrict internet access. Disabling firewall on localhost would not help.

One way to check this is telnet smtp.gmail.com 465. If this times out, then your problem is clear.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
1

Use port 587

Btw, having that throw ex in the catch block is really bad, you loose the stack trace. I am sure this was just for debugging purposes, but it would be better to just use throw without the ex to rethrow the same exception.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • Chris thanks, I treid just using throw and get the same error "An existing connection was forcibly closed by the remote host ". If I use port 587 I get this error but if I use port 465 time is out. plz guide – user576510 Jan 15 '11 at 07:01
  • @user576510, I tested with your exact code using port 587 and my account details of course, it worked fine. The comment I made about `throw ex` would not change the error, it was just general advice on a best practice so that the stacktrace info is not lost, not relevant to this issue but it is a best practice suggestion. – Chris Taylor Jan 15 '11 at 07:14
  • Chris any idea why it is not working on my machine ? thanks – user576510 Jan 15 '11 at 07:30
  • @user576510, that is difficult to guess. I would suggest, if possible, that you take your application and try run it from another machine on another network, maybe it is related to your network, firewall or something. One question, do you have an email client on the same machine that can successfully use the gmail smtp? – Chris Taylor Jan 15 '11 at 08:20