如何在发送邮件之前验证SMTP凭证?凭证、发送邮件、如何在、SMTP

2023-09-02 01:37:37 作者:痴人说梦.

我需要发送邮件之前验证在 SmtpClient 实例设置的用户名和密码。使用这个code:

I need to validate the username and password set in an SmtpClient instance before sending mail. Using this code:

SmtpClient client = new SmtpClient(host);
client.Credentials = new NetworkCredential(username,password);
client.UseDefaultCredentials = false;

// Here I need to verify the credentials(i.e. username and password)
client.Send(mail);

我如何可以验证标识的凭据是否允许用户连接和发送邮件?

How can I validate whether the user identified by the credentials is allowed to connect and send a mail?

推荐答案

有没有办法。

SmtpClient bascially无法验证usernamepassword而不接触送达连接。

SmtpClient bascially can not validate the usernamepassword without contacting the serve it connects to.

你可以做的就是做SmtpClient外,通过打开一个TCP连接到服务器...但认证可以根据服务器的配置是复杂的。

What you could do is do it outside SmtpClient, by opening a TCP connection to the server... but authentication CAN be complicated depending how the server is configured.

我会问为什么你需要知道发送过吗?正常的行为,恕我直言,是将包装发送适当的错误处理在这里捕获异常。

May I ask WHY you need to know that before sending? normal behavior IMHO would be to wrap sending in appropriate error handling to catch exceptions here.