我们可以使用 gmail 帐户从本地主机发送电子邮件吗?
我们可以使用 gmail smtp 从本地主机发送电子邮件吗?我正在尝试并收到错误操作已超时。
我正在尝试从过去 3 天开始从本地主机发送电子邮件。如果我使用 gmail 从托管服务器发送电子邮件,它工作正常,但它无法在本地主机上工作。我已经禁用了防火墙防病毒功能,但即便如此还是不幸。请指导我您是否曾经使用过 gmail 从本地主机发送电子邮件(不涉及任何服务器)
如果可能的话这是我的代码请指导我。请帮助我并指导我,我被困住了。
谢谢
protected void btnConfirm_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
message.To.Add("[email protected]");
message.From = new MailAddress("[email protected]");
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("[email protected]", "**mypassword**");
try
{
smtp.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
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("[email protected]");
message.From = new MailAddress("[email protected]");
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("[email protected]", "**mypassword**");
try
{
smtp.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您可以使用 gmail 从本地主机发送电子邮件。
我曾经写过一篇关于如何使用 gmail 发送电子邮件的博文。
粘贴我的博文中的代码片段。
这是工作代码,我经常使用它。
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.
如果错误是
操作超时
,则一种可能是网络防火墙阻止对指定主机/端口的传出访问。在有防火墙/代理服务器来限制互联网访问的办公室中就会出现这种情况。禁用本地主机上的防火墙没有帮助。检查此问题的一种方法是
telnet smtp.gmail.com 465
。如果超时,那么你的问题就清楚了。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.使用端口 587
顺便说一句,在 catch 块中使用
throw ex
确实很糟糕,你会丢失堆栈跟踪。我确信这只是为了调试目的,但最好只使用throw
而不使用ex
来重新抛出相同的异常。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 usethrow
without theex
to rethrow the same exception.