无法使用SMTP向外部域发送电子邮件

时间:2021-11-05 14:31:46

I am not able to send emails to external domain addresses like 'user.one@asdf.com' using the code below.

我无法使用以下代码向“user.one@asdf.com”等外部域地址发送电子邮件。

SmtpClient smtpClient = new SmtpClient(smtpMailServer);
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;

//Sending mail.
smtpClient.Send(mailMessage);

I get an exception -

我得到一个例外 -

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for xxx@example.com

信箱不可用。服务器响应为:5.7.1无法中继xxx@example.com

If I change the DeliveryMethod to -

如果我将DeliveryMethod更改为 -

smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

I am able to send the emails on my local machine. But it fails on the production site with an exception -

我可以在本地机器上发送电子邮件。但它在生产现场失败,但有例外 -

Cannot get IIS pickup directory

无法获取IIS拾取目录

Can you please suggest me what to do?

你能建议我做什么吗?

4 个解决方案

#1


4  

I had this issue and authenticating fixed it see below:

我遇到了这个问题并通过身份验证修复了它,如下所示

        SmtpClient client = new SmtpClient(EmailServer, 25);
        var SmtpUser = new System.Net.NetworkCredential("domain\\username", "password");
        client.Credentials = SmtpUser;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;

I had to use the double slash since one slash is the escape character so use two for it to work.

我不得不使用双斜杠,因为一个斜杠是转义字符,所以使用两个斜线来工作。

#2


1  

You usually need to authenticate with the external mail server using a username/password. As you are using an external server this will not know the credentials you are passing. This may be your issue.

您通常需要使用用户名/密码对外部邮件服务器进行身份验证。当您使用外部服务器时,这将不知道您传递的凭据。这可能是你的问题。

#3


1  

If you were to look up the MX record for the destination address (in your example, it is asdf.com) and then use that for the host property of SmtpClient, it should accept the message for delivery without authentication since it's to a local user. This is not easy to do since System.Net doesn't provide a managed DNS class that can return MX records but you can P/invoke unmanaged code to do it. Otherwise you will need to be sure that whatever SMTP server you are connecting to will relay for you and then set the Credentials property of SmtpClient to the appropriate credentials for connecting to that server. Setting the DeliveryMethod to PickupDirectoryFromIIS still only writes a file to the IIS pickup directory so it's only writing a file, it isn't doing an actual send.

如果您要查找目标地址的MX记录(在您的示例中,它是asdf.com),然后将其用于SmtpClient的主机属性,它应该接受邮件而不进行身份验证,因为它是本地用户。这并不容易,因为System.Net不提供可以返回MX记录的托管DNS类,但是您可以P /调用非托管代码来执行此操作。否则,您需要确保所连接的任何SMTP服务器都将为您进行中继,然后将SmtpClient的Credentials属性设置为用于连接到该服务器的相应凭据。将DeliveryMethod设置为PickupDirectoryFromIIS仍然只将文件写入IIS拾取目录,因此它只编写文件,而不是实际发送。

#4


0  

I faced this issue, which I solved by added a domain with "*.com" as domain name and type as "remote", under IIS 6.0 Manager/SMTP Virtual Server/Domains.

我遇到了这个问题,我通过在IIS 6.0管理器/ SMTP虚拟服务器/域下添加了一个域名“* .com”作为域名并输入“remote”来解决。

Although in my case the SMTP server allowed anonymous access.

虽然在我的情况下SMTP服务器允许匿名访问。

#1


4  

I had this issue and authenticating fixed it see below:

我遇到了这个问题并通过身份验证修复了它,如下所示

        SmtpClient client = new SmtpClient(EmailServer, 25);
        var SmtpUser = new System.Net.NetworkCredential("domain\\username", "password");
        client.Credentials = SmtpUser;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;

I had to use the double slash since one slash is the escape character so use two for it to work.

我不得不使用双斜杠,因为一个斜杠是转义字符,所以使用两个斜线来工作。

#2


1  

You usually need to authenticate with the external mail server using a username/password. As you are using an external server this will not know the credentials you are passing. This may be your issue.

您通常需要使用用户名/密码对外部邮件服务器进行身份验证。当您使用外部服务器时,这将不知道您传递的凭据。这可能是你的问题。

#3


1  

If you were to look up the MX record for the destination address (in your example, it is asdf.com) and then use that for the host property of SmtpClient, it should accept the message for delivery without authentication since it's to a local user. This is not easy to do since System.Net doesn't provide a managed DNS class that can return MX records but you can P/invoke unmanaged code to do it. Otherwise you will need to be sure that whatever SMTP server you are connecting to will relay for you and then set the Credentials property of SmtpClient to the appropriate credentials for connecting to that server. Setting the DeliveryMethod to PickupDirectoryFromIIS still only writes a file to the IIS pickup directory so it's only writing a file, it isn't doing an actual send.

如果您要查找目标地址的MX记录(在您的示例中,它是asdf.com),然后将其用于SmtpClient的主机属性,它应该接受邮件而不进行身份验证,因为它是本地用户。这并不容易,因为System.Net不提供可以返回MX记录的托管DNS类,但是您可以P /调用非托管代码来执行此操作。否则,您需要确保所连接的任何SMTP服务器都将为您进行中继,然后将SmtpClient的Credentials属性设置为用于连接到该服务器的相应凭据。将DeliveryMethod设置为PickupDirectoryFromIIS仍然只将文件写入IIS拾取目录,因此它只编写文件,而不是实际发送。

#4


0  

I faced this issue, which I solved by added a domain with "*.com" as domain name and type as "remote", under IIS 6.0 Manager/SMTP Virtual Server/Domains.

我遇到了这个问题,我通过在IIS 6.0管理器/ SMTP虚拟服务器/域下添加了一个域名“* .com”作为域名并输入“remote”来解决。

Although in my case the SMTP server allowed anonymous access.

虽然在我的情况下SMTP服务器允许匿名访问。