Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send emails to external domain using SMTP

I am not able to send emails to external domain addresses like '[email protected]' 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 [email protected]

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?

like image 584
Murali B Avatar asked Sep 29 '09 11:09

Murali B


People also ask

Why is SMTP not sending emails?

Check whether there is network access from CSO to the SMTP server. Check whether the firewall is blocking SMTP traffic to SMTP server or whether the ports are blocked. If the server settings and authentication settings are correct, check whether the firewall is blocking port 587 and 465 and SMTP traffic.

Can Office 365 SMTP relay work with external address?

We can configure Office 365 SMTP relay to enable LOB application sending emails to both internal and external users.


2 Answers

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.

like image 135
Andy Stannard Avatar answered Nov 07 '22 22:11

Andy Stannard


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.

like image 24
Jeff Tucker Avatar answered Nov 07 '22 23:11

Jeff Tucker