简体   繁体   English

使用Asp.net发送电子邮件异常

[英]Exception sending email using Asp.net

I've been tring to make a webpage use Gmail as a smtp Server to send emails with Asp.net. 我一直在努力使网页使用Gmail作为smtp服务器来通过Asp.net发送电子邮件。 Here is the link for the settings that google has set for such occasion. 这是谷歌为此设置的设置链接。 https://support.google.com/a/answer/176600?hl=en https://support.google.com/a/answer/176600?hl=zh_CN

Every time I try to send an email I get the "Exception caught in RetryIfBusy()" message.. 每次尝试发送电子邮件时,都会收到“ RetryIfBusy()中捕获了异常”消息。

Any ideas?...Here is my code below: 有什么想法吗?...下面是我的代码:

    [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());
        }
    } 

You are using a wrong port on Google's SMTP server. 您在Google的SMTP服务器上使用了错误的端口。 Should be: 应该:

smtp.Port = 587;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM