我通过gmail发送邮件时遇到此错误(试图通过其访问权限禁止访问套接字74.125.24.109:587)

问题描述:

这是我的代码



This is my code

MailMessage msgMail = new MailMessage();

MailMessage myMessage = new MailMessage();
myMessage.From = new MailAddress("<removed>@gmail.com", "<removed>@gmail.com");
myMessage.To.Add("<removed>6@yahoo.com");
myMessage.Subject = "Hello";
myMessage.IsBodyHtml = true;

myMessage.Body = "Message Body";


SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("<removed>@gmail.com", "<removed>");
mySmtpClient.Host = "smtp.gmail.com";
mySmtpClient.Port = 587;
mySmtpClient.UseDefaultCredentials = true;
mySmtpClient.EnableSsl = true;
mySmtpClient.Credentials = myCredential;
mySmtpClient.ServicePoint.MaxIdleTime = 1;

mySmtpClient.Send(myMessage);
myMessage.Dispose();





我的尝试:



使用gmail帐户

i关注这一点



What I have tried:

using gmail account
i follow this point

Step 1: Check that IMAP is turned on
On your computer, open Gmail.
In the top right, click Settings Settings.
Click Settings.
Click the Forwarding and POP/IMAP tab.
In the "IMAP Access" section, select Enable IMAP.
Click Save Changes.
Step 2: Change your IMAP settings in your email client
Use the table below to update your client with the correct information. For help updating your settings, search your email client's Help Center for instructions on setting up IMAP.

Incoming Mail (IMAP) Server	
imap.gmail.com

Requires SSL: Yes

Port: 993

Outgoing Mail (SMTP) Server	
smtp.gmail.com

Requires SSL: Yes

Requires TLS: Yes (if available)

Requires Authentication: Yes

Port for SSL: 465

Port for TLS/STARTTLS: 587

Full Name or Display Name	Your name
Account Name, User name, or Email address	Your full email address
Password	Your Gmail password

我发现有问题的一个问题是你为什么要使用 UseDefaultCredentials 然后同时传递你自己的凭据?正在使用更多的冗余呼叫,这使得电子邮件服务器变得可疑 - 与大多数国家的移民法相比,SMTP提供商及其法律更为严格。 :笑:



在大多数情况下,我将所有内容保留为默认值,请尝试以下代码,

One thing that I find problematic is why are you using UseDefaultCredentials and then at the same time also passing on your own credentials? Much more redundant calls are being used and which makes the email servers suspicious — SMTP providers and their laws are more strict, than the immigration laws of most countries. :laugh:

In most cases, I leave everything to the defaults, try the following code,
// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
   // Configure the client
   client.EnableSsl = true;
   client.Credentials = new NetworkCredential("<username>", "<password>");

   // A client has been created, now you need to create a MailMessage object
   MailMessage message = new MailMessage(
                            "from@example.com", // From field
                            "to@example.com", // Recipient field
                            "Hello", // Subject of the email message
                            "World!" // Email message body
                         );

   // Send the message
   client.Send(message);
}



这是我在文章中提供的工作代码, [ ^ ]并且只有在您的帐户是妥协或需要一些配置。


This is the working code, that I provided in my articles, Sending emails over .NET framework, and general problems – using C# code[^] and would only not work if your account is compromised or requires some configurations.