gpt4 book ai didi

c# - 我可以在调用 client.Send() 之前测试 SmtpClient 吗?

转载 作者:IT王子 更新时间:2023-10-29 04:37:00 24 4
gpt4 key购买 nike

这与我前几天在 how to send email 上提出的问题有关.

我的新问题是这样的...如果我的应用程序的用户在防火墙后面或其他一些原因导致 client.Send(mail) 行不起作用...

行后:

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

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

我考虑过将它放在 try/catch 循环中,但我宁愿先做一个测试,然后弹出一个对话框说:无法访问 smtp 或类似的东西。

(我假设我和我的应用程序用户都没有能力调整他们的防火墙设置。例如......他们在工作时安装应用程序并且在工作时无法控制他们的互联网)

-阿德娜

最佳答案

我认为,如果您要测试 SMTP,那您就是在寻找一种无需实际发送电子邮件即可验证您的配置和网络可用性的方法。任何方式都是我需要的,因为没有有意义的虚拟电子邮件。

在我的开发人员同事的建议下,我想出了这个解决方案。一个小的帮助类,使用如下。我在发送电子邮件的服务的 OnStart 事件中使用了它。

注意:TCP 套接字内容归功于 Peter A. Bromberg,地址为 http://www.eggheadcafe.com/articles/20030316.asp和配置读东西给这里的人:Access system.net settings from app.config programmatically in C#

助手:

public static class SmtpHelper
{
/// <summary>
/// test the smtp connection by sending a HELO command
/// </summary>
/// <param name="config"></param>
/// <returns></returns>
public static bool TestConnection(Configuration config)
{
MailSettingsSectionGroup mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings == null)
{
throw new ConfigurationErrorsException("The system.net/mailSettings configuration section group could not be read.");
}
return TestConnection(mailSettings.Smtp.Network.Host, mailSettings.Smtp.Network.Port);
}

/// <summary>
/// test the smtp connection by sending a HELO command
/// </summary>
/// <param name="smtpServerAddress"></param>
/// <param name="port"></param>
public static bool TestConnection(string smtpServerAddress, int port)
{
IPHostEntry hostEntry = Dns.GetHostEntry(smtpServerAddress);
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], port);
using (Socket tcpSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
//try to connect and test the rsponse for code 220 = success
tcpSocket.Connect(endPoint);
if (!CheckResponse(tcpSocket, 220))
{
return false;
}

// send HELO and test the response for code 250 = proper response
SendData(tcpSocket, string.Format("HELO {0}\r\n", Dns.GetHostName()));
if (!CheckResponse(tcpSocket, 250))
{
return false;
}

// if we got here it's that we can connect to the smtp server
return true;
}
}

private static void SendData(Socket socket, string data)
{
byte[] dataArray = Encoding.ASCII.GetBytes(data);
socket.Send(dataArray, 0, dataArray.Length, SocketFlags.None);
}

private static bool CheckResponse(Socket socket, int expectedCode)
{
while (socket.Available == 0)
{
System.Threading.Thread.Sleep(100);
}
byte[] responseArray = new byte[1024];
socket.Receive(responseArray, 0, socket.Available, SocketFlags.None);
string responseData = Encoding.ASCII.GetString(responseArray);
int responseCode = Convert.ToInt32(responseData.Substring(0, 3));
if (responseCode == expectedCode)
{
return true;
}
return false;
}
}

用法:

if (!SmtpHelper.TestConnection(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)))
{
throw new ApplicationException("The smtp connection test failed");
}

关于c# - 我可以在调用 client.Send() 之前测试 SmtpClient 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/372742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com