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

时间:2011-01-15 06:04:53

标签: c# asp.net smtp gmail gmail-imap

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

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

如果有可能,我的代码请指导我。请帮帮我,指导我,我被困了。

感谢

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

3 个答案:

答案 0 :(得分:5)

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

我曾写过a blogpost about how to send email using gmail

my blogpost粘贴代码段。

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

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

答案 1 :(得分:2)

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

检查此问题的一种方法是telnet smtp.gmail.com 465。如果超时,那么你的问题很明显。

答案 2 :(得分:1)

使用端口 587

顺便说一下,在catch块中有throw ex非常糟糕,你松开了堆栈跟踪。我确信这仅用于调试目的,但最好只使用throw而不使用ex来重新抛出相同的异常。

相关问题