我有一个标准的 Google Apps 帐户。 我已通过 Google Apps 设置了自定义域。 当我使用 Gmail 界面时,我能够通过 Google Apps 成功发送和接收电子邮件。 但是,我想通过代码发送电子邮件。 为了尝试这个,我一直在尝试以下代码:
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.Subject = "Test";
mailMessage.Body = "<html><body>This is a test</body></html>";
mailMessage.IsBodyHtml = true;
// Create the credentials to login to the gmail account associated with my custom domain
string sendEmailsFrom = "[email protected]";
string sendEmailsFromPassword = "password";
NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Timeout = 20000;
mailClient.Credentials = cred;
mailClient.Send(mailMessage);
当到达 Send 方法时,会抛出一个异常,指出:
“SMTP 服务器需要安全的
连接或客户端未连接
已验证。 服务器响应
是:5.5.1 需要身份验证。”
如何通过 Google 通过我的自定义域发送电子邮件?
I have a standard Google Apps account. I have setup a custom domain through Google Apps. I am able to send and receive emails successfully through Google Apps when I use the Gmail interface. However, I want to send an email via code. In order to attempt this, I have been trying the following code:
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.Subject = "Test";
mailMessage.Body = "<html><body>This is a test</body></html>";
mailMessage.IsBodyHtml = true;
// Create the credentials to login to the gmail account associated with my custom domain
string sendEmailsFrom = "[email protected]";
string sendEmailsFromPassword = "password";
NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Timeout = 20000;
mailClient.Credentials = cred;
mailClient.Send(mailMessage);
When the Send method is reached, an Exception is thrown that states:
"The SMTP server requires a secure
connection or the client was not
authenticated. The server response
was: 5.5.1 Authentication Required."
How do I send emails through my custom domain via Google?
发布评论
评论(5)
无需在代码中对所有 SMTP 设置进行硬编码。 将它们放在 web.config 中。 这样,您可以根据需要加密这些设置并即时更改它们,而无需重新编译应用程序。
发送电子邮件时结束,只需在 SmtpClient 上启用 SSL:
确保您从尝试在 Gmail 中进行身份验证的同一电子邮件地址发送电子邮件。
注意:从 .NET 4.0 开始,您可以将
enableSsl="true"
插入到web.config
中,而不是在代码中进行设置。There is no need to hardcode all SMTP settings in your code. Put them in web.config instead. This way you can encrypt these settings if needed and change them on the fly without recompiling your application.
End when you send email just enable SSL on your SmtpClient:
Make sure that you're sending email from the same email address with which you're trying to authenticate at Gmail.
Note: Starting with .NET 4.0 you can insert
enableSsl="true"
intoweb.config
as opposed to setting it in code.这就是我在 WPF 4
Watch for Firewalls and Anti-Viruses 中使用的内容。 这些令人毛骨悚然的事情往往会阻止应用程序发送电子邮件。
我使用 McAfee Enterprise,并且必须添加可执行文件名称(例如 Bazar.exe 和 Bazar.vshost.exe 的 Bazar.*)才能发送电子邮件...
This is what I use in WPF 4
Watch for Firewalls and Anti-Viruses. These creepy things tend to block applications sending email.
I use McAfee Enterprise and I have to add the executable name (like Bazar.* for both Bazar.exe and Bazar.vshost.exe) to be able to send emails...
将端口更改为465
change the port to 465
不需要做任何事情。 只需首次登录您当前的帐户并按照说明操作即可。
你的问题将会解决。 出现这种情况是因为您在Google Apps中创建了帐户但没有登录。 只需登录并按照说明进行尝试即可。
There is not need to do anything. Just login in your current account first time and follow instructions.
Your problem will resolve. It occur because you had created the account in google apps but did not login. Just login and follow the instructions and try.
我的代码在 Port=587 上使用 TLS 连接到 smtp.google.com(SSL 应为端口 465),但仍需要 EnableSsl=true
请注意您的 G SUITE 帐户上的这些重要先决条件
My code is connecting to smtp.google.com using TLS on Port=587 (SSL should be port 465) but still needs EnableSsl=true
Notice these important prerequisites on your G SUITE account