我有一个标准的 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 通过我的自定义域发送电子邮件?
谢谢!