Can we send email from the local host using a gmail account?

Can we send local email hosting using gmail smtp? I try and get an error. The operation has expired.

I am trying to send email from a local host in the last 3 days. It works great if I send emails from my hosting server using gmail, but it does not work on localhost. I disabled antivirus antivirus firewall, but even then no luck. Please let me know that you have ever used gmail to send emails from localhost (without any server)

If possible, here is my code, please help me. Plesi helped me and directed me, I am stuck.

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; } } 
+4
source share
3 answers

Yes, you can send email using gmail with localhost.

I once wrote a blog post on how to send email using gmail .

Paste the code snippet from my blogpost .

This is working code, and I use it often.

 /// <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; } } 
+5
source

If the Operation has timed out error, then one of the possibilities is that the network firewall blocks outgoing access to the specified host / port. This will be the case in offices that have a firewall / proxy server to restrict Internet access. Disabling the firewall on localhost would not help.

One way to verify this is telnet smtp.gmail.com 465 . If this time, then your problem is clear.

+2
source

Use port 587

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

+1
source

Source: https://habr.com/ru/post/1335644/


All Articles