简体   繁体   English

使用 SMTP 客户端发送电子邮件

[英]Sending an Email using SMTP client

I am trying to send an email using SMTP client using my gmail credential.我正在尝试使用我的 gmail 凭据使用 SMTP 客户端发送电子邮件。 This is the code am using这是我使用的代码

        using (var mail = GetMailInfo())
        {
            using (SmtpClient client = new SmtpClient("smtp.gmail.com", 465))
            {
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    client.Credentials = new NetworkCredential("**@gmail.com", "Password");
                    client.UseDefaultCredentials = false;
                    client.EnableSsl = true;
                    client.Send(mail);
            }
        }

I receive a time out error我收到超时错误

The operation has timed out操作已超时

If I try again I get another error saying如果我再试一次,我会收到另一个错误提示

Service not available, closing transmission channel.服务不可用,关闭传输通道。 The server response was: Too many concurrent SMTP connections;服务器响应为:并发 SMTP 连接过多; please try again later.请稍后再试。

What am I doing wrong?我究竟做错了什么?

At last I figure it out what was wrong with my code Updated code is below最后我弄清楚我的代码有什么问题更新的代码如下

using (var mail = GetMailInfo())
    {
        using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587)) // port is 587 instead of 465 as mentioned by @jdweng in the comment
        {
                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false; // this must be before the **Credentials** else this will reset the given credential                   
                client.Credentials = new NetworkCredential("**@gmail.com", "Password");
                
                client.EnableSsl = true;
                client.Send(mail);
        }
    }

On the top if you are using gmail go to settings and deactivate 2 way authentification and Access for less secure apps should be Activated在顶部,如果您使用的是 gmail,请转到设置并停用 2 路身份验证和访问不太安全的应用程序应该被激活

All these can be found Here所有这些都可以在这里找到

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

相关问题 使用smtp客户端向多个收件人发送电子邮件时抛出异常 - Throwing Exception while sending Email to multiple recipients using smtp client 使用SMTP发送电子邮件 - Sending out an email using SMTP 使用smtp客户端发送电子邮件时,如何解决smtp身份验证运行时错误? - How to fix the smtp authentication runtime error when sending email using smtp client? 使用smtp客户端发送带有附件错误的电子邮件 - sending email with smtp client with attachment giving error 通过SMTP客户端发送电子邮件时出错 - Error in Sending Email via a SMTP Client 通过SMTP客户端发送电子邮件失败,无一例外 - Sending email via SMTP client fails with no exception 服务器响应为:5.7.1使用SMTP客户端发送电子邮件时未对客户端进行身份验证 - The server response was: 5.7.1 Client was not authenticated While Sending email using SMTP client 使用gmail发送电子邮件时出错。 SMTP服务器需要安全连接或客户端未通过身份验证 - Error on sending email using gmail. The SMTP server requires a secure connection or the client was not authenticated GMail - 使用 smtp 客户端发送电子邮件时出错 - 错误 net_io_connectionclosed - GMail - Error sending email using smtp client - Error net_io_connectionclosed 使用 Amazon AWS SMTP 发送带有附件的电子邮件 - Sending email with attachments using Amazon AWS SMTP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM