Question

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;
    }
}
Was it helpful?

Solution

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;
    }
}

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top