67

我有一个标准的 Google Apps 帐户。我已经通过 Google Apps 设置了一个自定义域。使用 Gmail 界面时,我可以通过 Google Apps 成功发送和接收电子邮件。但是,我想通过代码发送电子邮件。为了尝试这个,我一直在尝试以下代码:

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("someone@somewhere.com");
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 = "emailAddress@mydomain.com";             
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 通过我的自定义域发送电子邮件?

谢谢!

4

5 回答 5

93

无需对代码中的所有 smtp 设置进行硬编码。将它们放在 web.config 中。这样,您可以在需要时加密这些设置并即时更改它们,而无需重新编译您的应用程序。

<configuration>
  <system.net>
    <mailSettings>
      <smtp from="example@domain.com" deliveryMethod="Network">
          <network host="smtp.gmail.com" port="587"
              userName="example@domain.com" password="password"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

在您发送电子邮件时结束,只需在您的 SmtpClient 上启用 SSL:

var message = new MailMessage("navin@php.net");
// here is an important part:
message.From = new MailAddress("example@domain.com", "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,而不是在代码中设置它。

于 2009-04-16T20:50:52.507 回答
8

这是我在 WPF 4 中使用的

SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("sender_email@domain.tld", "P@$$w0rD");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;

try 
{
    MailAddress maFrom = new MailAddress("sender_email@domain.tld", "Sender's Name", Encoding.UTF8),
    MailAddress maTo = new MailAddress("recipient_email@domain2.tld", "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;
}

注意防火墙和防病毒。这些令人毛骨悚然的事情往往会阻止应用程序发送电子邮件。我使用 McAfee Enterprise,我必须添加可执行文件名称(如 Bazar.exe 和 Bazar.vshost.exe 的 Bazar.*)才能发送电子邮件...

于 2011-01-02T08:02:30.500 回答
3

将端口更改为 465

于 2009-12-12T17:04:10.493 回答
3

没有必要做任何事情。只需第一次登录您当前的帐户并按照说明进行操作。

你的问题会解决的。这是因为您在谷歌应用程序中创建了帐户但没有登录。只需登录并按照说明进行操作即可。

于 2012-02-20T07:33:57.663 回答
0

我的代码使用端口=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 帐户的这些重要先决条件

于 2017-12-16T15:54:39.330 回答