通过C#通过谷歌Apps帐户发送电邮 [英] Send Email via C# through Google Apps account

查看:266
本文介绍了通过C#通过谷歌Apps帐户发送电邮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的谷歌Apps帐户。我已经安装通过谷歌Apps中的自定义域。我能够成功发送和接收电子邮件,通过谷歌企业应用套件当我使用Gmail界面。不过,我想通过code发送电子邮件。为了尝试此,我一直在尝试以下code:

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("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);

当达到发送方法,抛出异常的状态:

When the Send method is reached, an Exception is thrown that states:

SMTP服务器要求安全
  连接或客户端未
  验证。服务器响应
  是:5.5.1需要身份验证

"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?

谢谢!

推荐答案

有没有必要硬code在code所有SMTP设置。把它们放在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="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:

End when you send email just enable SSL on your SmtpClient:

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中进行身份验证的电子邮件地址发送电子邮件。

Make sure that you're sending email from the same email address with which you're trying to authenticate at Gmail.

注意的:在.NET 4.0开始,你可以插入enableSsl =真到web.config中,而不是在code设置它

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

这篇关于通过C#通过谷歌Apps帐户发送电邮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆