Unable to send emails to external domain using SMTP

我的未来我决定 提交于 2019-12-12 08:41:33

问题


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

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

If I change the DeliveryMethod to -

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

Can you please suggest me what to do?


回答1:


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:


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.




回答3:


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.




回答4:


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.

Although in my case the SMTP server allowed anonymous access.



来源:https://stackoverflow.com/questions/1491915/unable-to-send-emails-to-external-domain-using-smtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!