我为我的企业设置了一个谷歌帐户,例如 myCompanyName@gmail.com。我用它作为中继。您必须将您的谷歌帐户设置为“允许不太安全的应用程序”。
这是我的代码,可让潜在客户填写联系我们并将信息发送给我(即使在我发布到 Azure 时也可以工作:)):
private void SendEmailToMyCompany(ContactInfo contactInfo)
{
string message = contactInfo.Message.Replace("\n", "<br />");
MailAddress from = new MailAddress(contactInfo.Email);
MailAddress to = new MailAddress("myhotmailaccount@hotmail.com");
MailMessage mailMessage = new MailMessage(from, to);
StringBuilder body = new StringBuilder();
body.AppendFormat($"<b>First Name:</b> {contactInfo.FirstName}");
body.Append("<br />");
body.AppendFormat($"<b>Last Name:</b> {contactInfo.LastName}");
body.Append("<br />");
body.AppendFormat($"<b>Phone:</b> {contactInfo.Phone}");
body.Append("<br />");
body.AppendFormat($"<b>Email:</b> {contactInfo.Email}");
body.Append("<br />");
body.AppendFormat($"<b>Message:</b><br /><br /> {message}");
mailMessage.Body = body.ToString();
mailMessage.Subject = "MyCompany Customer Contact";
mailMessage.IsBodyHtml = true;
string smtpHost = _config["EmailSettings:SmtpHost"];
string port = _config["EmailSettings:Port"];
string userName = _config["EmailSettings:UserName"];
string password = _config["EmailSettings:Password"];
SmtpClient client = new SmtpClient(smtpHost)
{
Port = int.Parse(port),
Credentials = new NetworkCredential(userName, password),
EnableSsl = true
};
client.Send(mailMessage);
}
然后这是我在 app.config 中的电子邮件设置:
"EmailSettings": {
"SmtpHost": "smtp.gmail.com",
"Port": 587,
"UserName": "myCompanyNameGmailAccount@gmail.com",
"Password": "**********"
}