4

我们可以使用 gmail smtp 从本地主机发送电子邮件吗?我正在尝试并收到错误操作已超时。

我正在尝试从过去 3 天的本地主机发送电子邮件。如果我使用 gmail 从我的托管服务器发送电子邮件,它工作正常,但它不适用于 localhost。我已经禁用了防火墙防病毒软件,但即便如此也不走运。请指导我您是否曾经使用过 gmail 从本地主机发送电子邮件(不涉及任何服务器)

如果可能这是我的代码,请指导我。请帮助我并指导我,我被困住了。

谢谢

 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

3 回答 3

5

是的,您可以使用 gmail 从 localhost 发送电子邮件。

我曾经写过一篇关于如何使用 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("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;
    }
}
于 2011-01-15T06:37:36.633 回答
2

如果错误是Operation has timed out,则一种可能性是网络防火墙阻止了对指定主机/端口的传出访问。在有防火墙/代理服务器来限制互联网访问的办公室中就是这种情况。在 localhost 上禁用防火墙无济于事。

检查这一点的一种方法是telnet smtp.gmail.com 465. 如果超时,那么您的问题就很清楚了。

于 2011-01-15T06:23:59.590 回答
1

使用端口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.

于 2011-01-15T06:41:07.090 回答