SmtpClient - Send to different domains
19:50 27 Feb 2012

I have an SmtpClient which I pointed to my company's mail server. I am able to successfully send emails to/from address on that domain.

However, I need to do password resets and notifications for users of an app. Those users sign up using their own email address (multiple different domains).

How can I send an email to a user of a different domain? Every time I try (using my live.ca email), I get the following error:

SmtpFailedRecipientsException - Mailbox unavailable. The server response was: 5.7.1 Unable to relay

C# Code

SmtpClient mailClient = new SmtpClient();
MailMessage email = new MailMessage
{
    Subject = "Testing Mail",
    Body = "Testing Mail",
    From = new MailAddress("myAddress@myCompany.com")
};
email.To.Add(new MailAddress("myAddress@live.ca"));
mailClient.Send(email);

Web.config Code

  

  
    
  

Update:

I've got it working using the following methods:

1) Web.config


2) C# Code

mailClient.Credentials = new NetworkCredential("myNetworkUsername", "myPassword");
// or
mailClient.Credentials = CredentialCache.DefaultNetworkCredentials;

The 2nd line uses the credentials of whoever is logged in. It worked for local host but not when I deployed it.

I'm thinking of creating a default account for handling the app's mail and putting the username/pw in the web config, but this doesn't seem like the most secure practice. I'm still looking into the alternatives.

Update:

The code stopped working when my company switched our ISP to Shaw. We think Shaw may be blocking a port on our Exchange Server.

c# email smtp dns smtpclient