Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发送电子邮件捕获错误_C#_Email - Fatal编程技术网

C# 发送电子邮件捕获错误

C# 发送电子邮件捕获错误,c#,email,C#,Email,除了电子邮件未发送外,我如何拍摄 我有一个课程可以发送电子邮件,如下所示: public class Email { private SmtpClient SMTPCliente; private string From; public Email(string smtp, int port, bool ssl = false) { SMTPCliente = new SmtpClient(smtp, port); SMTPCliente.EnableSsl = ssl; S

除了电子邮件未发送外,我如何拍摄

我有一个课程可以发送电子邮件,如下所示:

public class Email {
 private SmtpClient SMTPCliente;
 private string From;

 public Email(string smtp, int port, bool ssl = false) {
  SMTPCliente = new SmtpClient(smtp, port);
  SMTPCliente.EnableSsl = ssl;
  SMTPCliente.DeliveryMethod = SmtpDeliveryMethod.Network;
  SMTPCliente.Timeout = 5000;
 }

 public void Login(string username, string password) {
  From = username;
  SMTPCliente.Credentials = new NetworkCredential(username, password);
 }

 public void Send(string message, string assunto, string destinatario, string attach = "") {
  MailMessage msg = new MailMessage();
  msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  msg.IsBodyHtml = true;
  msg.From = new MailAddress(From);
  msg.Body = message;
  msg.Subject = assunto;
  msg.To.Add(destinatario);
  if (!string.IsNullOrEmpty(attach))
   msg.Attachments.Add(new Attachment(attach));

  SMTPCliente.Send(msg);

  //msg.Dispose();
  //msg.Attachments.Dispose();
 }
我正在使用try,catch块来尝试捕获异常,但是,什么都没有发生。。。程序无限运行,不会引发任何错误

try {
 Email email = new Email("smtp.live.com", 10);
 email.Login("this@gmail.com", "password");
 email.Send("My message", "Assunto", "destino@email.com");
} catch (FormatException) {
 //invalid email pattern
 //A cadeia de caracteres especificada não está no formato necessário para um endereço de email.
} catch (ArgumentException) {
 //no attachment
 //O parâmetro 'fileName' não pode ser uma cadeia de caracteres vazia.
} catch (Exception ex) {
 MessageBox.Show(ex.Message.ToString());
}

虚拟smtp数据仅用于案例

是否出现错误?我删除了端口,现在引发超时异常。。。