我想在C#上使用SMTP發送電子郵件,但我收到錯誤「發送郵件失敗」 我只想問我的代碼下面是正確的如何在使用SMTP發送電子郵件時解決「發送郵件失敗」錯誤
private void SendMail()
{
MailMessage mail = new MailMessage();
SmtpClient mailClient = new SmtpClient();
mail.From = new MailAddress(MailConst.From);
mail.To.Add(new MailAddress("[email protected]"));
mail.Subject = "Test";
mail.Body = "This is a test";
mailClient.Host = MailConst.SmtpServer;
mailClient.UseDefaultCredentials = false;
mailClient.Port = 465;
mailClient.EnableSsl = true;
mailClient.Credentials = new NetworkCredential(MailConst.Username, MailConst.Password);
try
{
mailClient.Send(mail);
}
catch(Exception ex)
{
WriteErrorOutput(ex.Message);
}
}
public class MailConst
{
public static string Username = "user";
public static string Password = "pass";
public const string SmtpServer = "smtp.gmail.com";
public static string From = Username + "@gmail.com";
}
我一直在得到異常「無法發送消息」。
而當我嘗試打開異常以查看有什麼問題時,它顯示「Command'Debug.QuickWatch'不可用。」
欣賞你的時間和你的幫助回答我的問題。
謝謝。
編輯:
終於能看到內部異常,好像我的連接被拒絕。 檢查了我們的網絡管理員,他說Gmail SMTP在我們的網絡中被阻止。
感謝您的幫助球員。
您需要發佈內部異常可能有很多原因,你爲什麼得到這個錯誤 – Rohit 2015-03-19 05:33:15