我一直想让网页使用 Gmail 作为 smtp服务器来发送电子邮件与Asp.net。以下是谷歌为此类场合设置的设置的链接。 https://support.google.com/a/answer/176600?hl=en
每当我尝试发送电子邮件时,我都会收到“RetryIfBusy()中的异常”消息..
有什么想法?...以下是我的代码:
[System.Web.Services.WebMethod]
public static void SendMessage(string toEmail)
{
MailMessage msg = new MailMessage("myEmail@gmail.com", toEmail);
msg.Subject = "Email Subject";
msg.Body = "Here goes email body";
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
System.Net.NetworkCredential netCred = new System.Net.NetworkCredential();
netCred.UserName = "myEmail@gmail.com";
netCred.Password = "myPassword";
smtp.Credentials = netCred;
smtp.Port = 465;
smtp.EnableSsl = true;
try
{
smtp.Send(msg);
Console.WriteLine("Email Successfully sent!!");
}
catch (SmtpFailedRecipientsException ex)
{
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable)
{
Console.WriteLine("Delivery failed - retrying in 5 seconds.");
System.Threading.Thread.Sleep(5000);
smtp.Send(msg);
}
else
{
Console.WriteLine("Failed to deliver message to " +
ex.InnerExceptions[i].FailedRecipient);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in RetryIfBusy(): " +
ex.ToString());
}
}
答案 0 :(得分:0)
您在Google的SMTP服务器上使用了错误的端口。应该是:
smtp.Port = 587;