通过 Google Apps 帐户通过 C# 发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/757987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send Email via C# through Google Apps account
提问by user70192
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:
我有一个标准的 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);
When the Send method is reached, an Exception is thrown that states:
当到达 Send 方法时,会抛出一个异常,指出:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
“SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。”
How do I send emails through my custom domain via Google?
如何通过 Google 通过我的自定义域发送电子邮件?
Thanks!
谢谢!
采纳答案by Konstantin Tarkus
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.
无需对代码中的所有 smtp 设置进行硬编码。将它们放在 web.config 中。通过这种方式,您可以在需要时加密这些设置,并在不重新编译应用程序的情况下即时更改它们。
<configuration>
<system.net>
<mailSettings>
<smtp from="[email protected]" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587"
userName="[email protected]" password="password"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
End when you send email just enable SSL on your SmtpClient:
发送电子邮件时结束,只需在 SmtpClient 上启用 SSL:
var message = new MailMessage("[email protected]");
// here is an important part:
message.From = new MailAddress("[email protected]", "Mailer");
// it's superfluous part here since from address is defined in .config file
// in my example. But since you don't use .config file, you will need it.
var client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
Make sure that you're sending email from the same email address with which you're trying to authenticate at Gmail.
确保您发送的电子邮件地址与您尝试在 Gmail 中进行身份验证的电子邮件地址相同。
Note: Starting with .NET 4.0 you can insert enableSsl="true" into web.config as opposed to setting it in code.
注意:从 .NET 4.0 开始,您可以将 enableSsl="true" 插入 web.config,而不是在代码中设置它。
回答by sf.
change the port to 465
将端口更改为 465
回答by Achilles
This is what I use in WPF 4
这是我在 WPF 4 中使用的
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("[email protected]", "P@$$w0rD");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try {
MailAddress
maFrom = new MailAddress("[email protected]", "Sender's Name", Encoding.UTF8),
maTo = new MailAddress("[email protected]", "Recipient's Name", Encoding.UTF8);
MailMessage mmsg = new MailMessage(maFrom.Address, maTo.Address);
mmsg.Body = "<html><body><h1>Some HTML Text for Test as BODY</h1></body></html>";
mmsg.BodyEncoding = Encoding.UTF8;
mmsg.IsBodyHtml = true;
mmsg.Subject = "Some Other Text as Subject";
mmsg.SubjectEncoding = Encoding.UTF8;
client.Send(mmsg);
MessageBox.Show("Done");
} catch (Exception ex) {
MessageBox.Show(ex.ToString(), ex.Message);
//throw;
}
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...
注意防火墙和防病毒软件。这些令人毛骨悚然的事情往往会阻止应用程序发送电子邮件。我使用 McAfee Enterprise,并且必须添加可执行文件名称(例如 Bazar.exe 和 Bazar.vshost.exe 的 Bazar.*)才能发送电子邮件...
回答by Jeevan
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.
您的问题将得到解决。发生这种情况是因为您在 google 应用程序中创建了帐户但没有登录。只需登录并按照说明进行操作即可。
回答by Christian Ernst Rysgaard
My code is connecting to smtp.google.com using TLS on Port=587 (SSL should be port 465) but still needs EnableSsl=true
我的代码在端口=587(SSL 应该是端口 465)上使用 TLS 连接到 smtp.google.com,但仍然需要 EnableSsl=true
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = "INSERT EMAIL";
credentials.Password = "INSERT PASSWORD";
smtp.Credentials = credentials;
MailAddress addressFrom = new MailAddress(credentials.UserName);
MailAddress addressTo = new MailAddress("INSERT RECIPIENT");
MailMessage msg = new MailMessage(addressFrom, addressTo);
msg.Subject = "SUBJECT"
msg.Body = "BODY";
smtp.Send(msg);
Notice these important prerequisites on your G SUITE account
请注意您的 G SUITE 帐户的这些重要先决条件
- Ensure that the username you use has cleared the CAPTCHA word verification test that appears when you first sign in.
- Ensure that the account has a secure password - https://support.google.com/accounts/answer/32040
- Make sure that Less secure apps is enabled for the desired account- https://support.google.com/a/answer/6260879
- 确保您使用的用户名已通过首次登录时出现的 CAPTCHA 字词验证测试。
- 确保帐户具有安全密码 - https://support.google.com/accounts/answer/32040
- 确保为所需帐户启用了安全性较低的应用程序 - https://support.google.com/a/answer/6260879