Unable to send emails to external domain using SMTP

Murali B picture Murali B · Sep 29, 2009 · Viewed 24.6k times · Source

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?

Answer

Andy Stannard picture Andy Stannard · Apr 20, 2011

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.