【问题标题】:How to solve "Failure Sending Mail" error while sending email using SMTP使用 SMTP 发送电子邮件时如何解决“发送邮件失败”错误
【发布时间】:2015-03-19 04:57:36
【问题描述】:

我正在尝试在 C# 上使用 SMTP 发送电子邮件,但我收到错误“发送邮件失败” 我想问一下我下面的代码是否正确

    private void SendMail()
    {
        MailMessage mail = new MailMessage();
        SmtpClient mailClient = new SmtpClient();

        mail.From = new MailAddress(MailConst.From);
        mail.To.Add(new MailAddress("recepient@gmail.com"));
        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";

    }

我不断收到“发送消息失败”异常。

当我尝试打开异常查看问题时,它显示“命令 'Debug.QuickWatch' 不可用。”

感谢您在回答我的问题时花费的时间和帮助。

谢谢。

编辑:

终于可以看到内部异常了,好像我的连接被拒绝了。 与我们的网络管理员核实,他说 Gmail SMTP 在我们的网络中被阻止。

感谢大家的帮助。

【问题讨论】:

  • 您需要发布内部异常可能有很多原因导致您收到此错误

标签: c# email smtp


【解决方案1】:

我认为我们需要检查连接

要在您的计算机上运行 telnet 并在 Windows 上进行测试:

1.Open the Start menu, and select Run.
2.Enter command in the Open: field, and click OK.
3.Enter 'telnet smtp.gmail.com 465,' and hit Enter, or enter 'telnet smtp.gmail.com 587' instead.

【讨论】:

    【解决方案2】:

    试试这个..

      private void SendMail()
      {
        MailMessage mail = new MailMessage();
        SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
    
        mail.From = new MailAddress(MailConst.From);
        mail.To.Add(new MailAddress("recepient@gmail.com"));
        mail.Subject = "Test";
        mail.Body = "This is a test";
    
        mailClient.EnableSsl = true;
        mailClient.Credentials = new NetworkCredential(MailConst.Username, MailConst.Password);
        try
        {
            mailClient.Send(mail);
        }
        catch(Exception ex)
        {
            WriteErrorOutput(ex.Message);
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 2015-04-16
      • 2015-08-27
      • 2011-07-30
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多