Search code examples
c#asp.netsmtpgmailgmail-imap

can we send email from local host using gmail account?


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

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