通过 Google Apps 帐户通过 C# 发送电子邮件

发布于 2024-07-17 09:12:48 字数 1400 浏览 9 评论 0 原文

我有一个标准的 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?

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

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

发布评论

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

评论(5

空名 2024-07-24 09:12:48

无需在代码中对所有 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>

发送电子邮件时结束,只需在 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);

确保您从尝试在 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.

<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:

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.

Note: Starting with .NET 4.0 you can insert enableSsl="true" into web.config as opposed to setting it in code.

时光沙漏 2024-07-24 09:12:48

这就是我在 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),
    MailAddress maTo = new MailAddress("[email protected]", "Recipient's Name", Encoding.UTF8);
    MailMessage mmsg = new MailMessage(maFrom, maTo);
    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 中使用的内容。 这些令人毛骨悚然的事情往往会阻止应用程序发送电子邮件。
我使用 McAfee Enterprise,并且必须添加可执行文件名称(例如 Bazar.exe 和 Bazar.vshost.exe 的 Bazar.*)才能发送电子邮件...

This is what I use in 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),
    MailAddress maTo = new MailAddress("[email protected]", "Recipient's Name", Encoding.UTF8);
    MailMessage mmsg = new MailMessage(maFrom, maTo);
    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...

如歌彻婉言 2024-07-24 09:12:48

将端口更改为465

change the port to 465

温折酒 2024-07-24 09:12:48

不需要做任何事情。 只需首次登录您当前的帐户并按照说明操作即可。

你的问题将会解决。 出现这种情况是因为您在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.

花之痕靓丽 2024-07-24 09:12:48

我的代码在 Port=587 上使用 TLS 连接到 smtp.google.com(SSL 应为端口 465),但仍需要 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);

请注意您的 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

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

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