我们可以使用 gmail 帐户从本地主机发送电子邮件吗?

发布于 2024-10-12 07:01:17 字数 1481 浏览 8 评论 0原文

我们可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-10-19 07:01:17

是的,您可以使用 gmail 从本地主机发送电子邮件。

我曾经写过一篇关于如何使用 gmail 发送电子邮件的博文

粘贴我的博文中的代码片段。

这是工作代码,我经常使用它。

/// <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("[email protected]", "YourGmailPassword");

        //enable ssl
        mailClient.EnableSsl = true;

        // Create the mail message (from, to, subject, body)
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress("[email protected]");
        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;
    }
}

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("[email protected]", "YourGmailPassword");

        //enable ssl
        mailClient.EnableSsl = true;

        // Create the mail message (from, to, subject, body)
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress("[email protected]");
        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;
    }
}
初心未许 2024-10-19 07:01:17

如果错误是操作超时,则一种可能是网络防火墙阻止对指定主机/端口的传出访问。在有防火墙/代理服务器来限制互联网访问的办公室中就会出现这种情况。禁用本地主机上的防火墙没有帮助。

检查此问题的一种方法是 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.

只是在用心讲痛 2024-10-19 07:01:17

使用端口 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 use throw without the ex to rethrow the same exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文