在调用client.Send()之前,可以测试SmtpClient吗? [英] Can I test SmtpClient before calling client.Send()?

查看:106
本文介绍了在调用client.Send()之前,可以测试SmtpClient吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我之前在如何发送电子邮件

我的新的相关问题是这个...如果我的应用程序的用户在防火墙后面或为什么线client.Send(邮件)赢了

My new, related question is this... what if the user of my application is behind a firewall or some other reason why the line client.Send(mail) won't work...

行后:

SmtpClient client = new SmtpClient("mysmtpserver.com", myportID);
client.Credentials = new System.Net.NetworkCredential("myusername.com", "mypassword");

在尝试发送之前,我可以做些什么来测试客户端?

is there something I can do to test client before I try sending?

我以为把它放在一个try / catch循环中,但我宁愿做一个测试,然后弹出一个对话框,说:无法访问smtp或某事像那样。

I thought about putting this in a try/catch loop, but I'd rather do a test and then pop up a dialog saying: can't access smtp or something like that.

(我假设我或者我的应用程序用户都无法调整其防火墙设置,例如...他们在工作中安装该应用程序并且在工作中不能控制他们的互联网)

(I'm presuming that neither I, nor potentially my application user, has the ability to adjust their firewall settings. For example... they install the app at work and don't have control over their internet at work)

-Adeena

推荐答案

我认为这是一个例外处理是首选解决方案的情况。你真的不知道它会工作,直到你尝试,失败是一个例外。

I think this is a case where exception handling would be the preferred solution. You really don't know that it will work until you try, and failure is an exception.

编辑:

你将要处理SmtpException。这有一个StatusCode属性,它是一个枚举,它将告诉你为什么Send()失败。

You'll want to handle SmtpException. This has a StatusCode property, which is an enum that will tell you why the Send() failed.

这篇关于在调用client.Send()之前,可以测试SmtpClient吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆