简体   繁体   English

无法使用Gmail帐户在asp.net中发送电子邮件

[英]couldn't send email in asp.net using gmail account

Can we use gmail account to send email in asp.net website from * localhost * (local machine) ? 我们可以使用gmail帐户从* 本地主机* (本地计算机)在asp.net网站中发送电子邮件吗? I am trying but badly unsuccessful. 我正在尝试但严重失败。 It works fine on hosting but donot work on my machine. 它在托管上工作正常,但在我的计算机上不起作用。

I have windows server 2003 on my machine, I have added port 587 and 465 in firewall in exceptions. 我的机器上装有Windows Server 2003,在例外情况下,我在防火墙中添加了端口587和465。 In my gmail account I also have enabled POP and IMAP. 在我的Gmail帐户中,我还启用了POP和IMAP。 Some people suggest to use port 465 and others say port 587 should be used. 有人建议使用端口465,而其他人则建议使用端口587。 I tried both and below was my result: 我尝试了以下两种方法,结果是:

  1. Using port 465 it take time and finally give message that the opration has timed out. 使用端口465会花费一些时间,并最终提示操作已超时。 falure 虚假
  2. Using port 587 it dont take time, show message "failuer sending email" with an inner expection "No connection could be made because the target machine actively refused it 72.14.213.109:587" 使用端口587,它不花时间,显示消息“失败者发送电子邮件”,并带有内在的期望:“无法建立连接,因为目标计算机主动拒绝它72.14.213.109:587”

Below is my code, please guide me where I am wrong or what I should do. 以下是我的代码,请指导我在哪里出错或应该怎么做。

thanks 谢谢

public static bool SendMail(string gMailAccount, string password, string to, string subject, string message)
{
    try
    {
        NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(gMailAccount);
        msg.To.Add(new MailAddress(to));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = true;
        SmtpClient client = new SmtpClient("smtp.gmail.com");
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = loginInfo;
        client.Port = 587;
        client.Send(msg);

        return true;
    }
    catch (Exception e)
    {

        return false;
    }

}

I use smtp.yandex.com. 我使用smtp.yandex.com。 May be need change settings in your gmail account. 您的Gmail帐户中可能需要更改设置。

Try this firs: add: 试试这个冷杉:添加:

 client.DeliveryMethod = SmtpDeliveryMethod.Network;

If that does not help, your mailaddress might not be complete, use 如果这样做没有帮助,则您的邮件地址可能不完整,请使用

var fromAddress = new MailAddress("from@gmail.com", "From Name"); 

and in the credentials use 并在凭据中使用

Credentials = new NetworkCredential(fromAddress.Address, ....

Hope that helps 希望能有所帮助

This SmtpClient has always worked for me: 这个SmtpClient一直为我工作:

new SmtpClient
     {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential("noreply@my_domain.com", "password")
     };

And it matches yours. 它与您的相匹配。

Have you been able to contact smtp.gmail.com on port 587 through any other app? 您是否能够通过任何其他应用程序通过端口587与smtp.gmail.com联系? Telnet on that port maybe? 可以在该端口上进行Telnet? Am thinking it's probably a network issue, although you did state that your firewall was wide open it's not the only hop between you and google. 我以为这可能是网络问题,尽管您确实指出防火墙是完全开放的,但这并不是您和Google之间的唯一跳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM