我試圖用以下代碼從服務器發送帶有gmailaccount的郵件。c#gmail連接失敗,因爲連接的主機未能響應
var fromAddress = new MailAddress("[email protected]", "xxxxxx");
var toAddress = new MailAddress(toMailAddress);
const string fromPassword = "xxxxx";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
IsBodyHtml = true,
Subject = mailSubject,
Body = htmlBody
})
{
smtp.Send(message);
}
這讓我部署我的Windows服務器上,雖然我有一個Inobound規則在防火牆上的端口587的下一個結果。當我測試它的本地
看看這個問題也學會使用調試器來逐步通過您的代碼 - [使用C#發送郵件到Gmail帳戶](https://stackoverflow.com/questions/29251189/sending-mail-using -gmail-smtp-from-csharp-net) – MethodMan
[使用C#通過Gmail SMTP服務器發送電子郵件的可能的副本](https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-帶着-c-sharp) –
根據OP對以下奇怪接受的答案的評論,我正在投票決定將其作爲本地化版本,因爲OPs託管公司在防火牆中開放了一個端口。 –