通过 Google SMTP 发送邮件失败

发布于 2024-12-10 06:56:19 字数 1238 浏览 0 评论 0原文

我仍然收到“发送邮件失败”的消息。例外。内部异常是“无法连接到远程服务器”,内部异常是“连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应” ”。我很确定这不是防火墙设置的原因。有谁知道我能做什么?谢谢。

var mail = new MailMessage("[email protected]", "[email protected]")
                        {
                            Subject = "Testing subject",
                            Body = "Testing body"
                        };
            try
            {
                var client = new SmtpClient("smtp.google.com", 465)
                                {
                                    EnableSsl = true,
                                    Credentials = new NetworkCredential("[email protected]", "password")
                                };
                client.Send(mail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

I'm still getting "Failure sending mail." exception. The inner exception is "Unable to connect to the remote server" and the inner exception of that is "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond". I'm pretty sure the cause of this is not the firewall setting. Does anyone know what I can do about it? Thanks.

var mail = new MailMessage("[email protected]", "[email protected]")
                        {
                            Subject = "Testing subject",
                            Body = "Testing body"
                        };
            try
            {
                var client = new SmtpClient("smtp.google.com", 465)
                                {
                                    EnableSsl = true,
                                    Credentials = new NetworkCredential("[email protected]", "password")
                                };
                client.Send(mail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

手长情犹 2024-12-17 06:56:19

以下是所需的 Google 设置:

  • 传入邮件 (POP3) 服务器 - 需要 SSL:pop.gmail.com

    使用 SSL:是

    端口:995

    发送邮件 (SMTP) 服务器 - 需要 TLS3 或 SSL:smtp.gmail.com
    (使用身份验证)

    使用身份验证:是

    TLS/STARTTLS 端口:587

    SSL 端口:465

    帐户名称:您的完整电子邮件地址(包括@gmail.com 或
    @your_domain.com)

    电子邮件地址:您的电子邮件地址([电子邮件受保护]
    用户名@your_domain.com)

    密码:您的 Gmail 密码

Here is the Google settings needed:

  • Incoming Mail (POP3) Server - requires SSL: pop.gmail.com

    Use SSL: Yes

    Port: 995

    Outgoing Mail (SMTP) Server - requires TLS3 or SSL: smtp.gmail.com
    (use authentication)

    Use Authentication: Yes

    Port for TLS/STARTTLS: 587

    Port for SSL: 465

    Account Name: your full email address (including @gmail.com or
    @your_domain.com)

    Email Address: your email address ([email protected] or
    username@your_domain.com)

    Password: your Gmail password

却一份温柔 2024-12-17 06:56:19

我认为 465 不是正确的端口。你试过587吗? SMTP 服务器是 smtp.gmail.com

I don't think 465 is the right port. Have you tried 587? And the SMTP server is smtp.gmail.com.

别把无礼当个性 2024-12-17 06:56:19

我也在使用Godaddy电子邮件服务器,下面的代码对我来说效果很好:

命名空间

System.Net.Mail

============

string senderID = "[email protected]";
string senderPassword = "123456";
string body = " Test email ";

MailMessage mail = new MailMessage();
mail.To.Add(username);
//mail.CC.Add(_cc);
mail.From = new MailAddress(senderID);
mail.Priority = MailPriority.High;
mail.Subject = "Test Email";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "relay-hosting.secureserver.net"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
     (senderID, senderPassword); // ***use valid credentials***
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.Send(mail);

I am also working with Godaddy email server and below code worked fine for me:

Namespace:

System.Net.Mail

============

string senderID = "[email protected]";
string senderPassword = "123456";
string body = " Test email ";

MailMessage mail = new MailMessage();
mail.To.Add(username);
//mail.CC.Add(_cc);
mail.From = new MailAddress(senderID);
mail.Priority = MailPriority.High;
mail.Subject = "Test Email";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "relay-hosting.secureserver.net"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
     (senderID, senderPassword); // ***use valid credentials***
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.Send(mail);
╰ゝ天使的微笑 2024-12-17 06:56:19

如果主机名正确并且谷歌支持基于 SSL 的 SMTP,那么它会被某些东西阻止,我将开始检查防火墙。

If the hostname is correct and google supports SMTP over SSL, then it is being blocked by something and I would start checking firewalls.

蓝海 2024-12-17 06:56:19

我不认为 smtp.google.com 是正确的 Gmail 服务器。您是否在端口 25 上尝试过 smtp.gmail.com?

端口 465 用于通过 SSL 的 SMTP,.NET SmtpClient 不支持该端口。相反,请使用端口 25。SMTP 客户端将使用 STARTTLS 功能来加密通信。

I don't think smtp.google.com is right server for gmail. Have you tried smtp.gmail.com on port 25?

Port 465 is for SMTP via SSL, which is not supported by the .NET SmtpClient. Instead, use port 25. The SMTP Client will use the STARTTLS feature to encrypt the communication.

晌融 2024-12-17 06:56:19

smtpclient 类的正确端口是 587

the correct port is 587 for smtpclient class

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