2

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

  <system.net>
<mailSettings>
  <smtp>
    <network host="mail.myCompany.com" port="25" userName="myAddress@myCompany.com" password="myPassword" defaultCredentials="false"/>
  </smtp>
</mailSettings>

Update:

I've got it working using the following methods:

1) Web.config

<network host="mail.myCompany.com" port="25" userName="myNetworkUsername" password="myPassword" defaultCredentials="false" />

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.

Lifes
  • 1,226
  • 2
  • 25
  • 45

1 Answers1

0

Here's an article that explains the use of port 587 for sending emails outside of domains: http://mostlygeek.com/tech/smtp-on-port-587

ron tornambe
  • 10,452
  • 7
  • 33
  • 60
  • Hmm. I just tried this on one of my sites and it solved a similar problem. The only difference I can see is that the "from" is specified within the SMTP element: – ron tornambe Feb 28 '12 at 03:01
  • Just tried that and got a SMTP Exception: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated. Thanks for the input, I'm going to try the other suggestions now. – Lifes Feb 29 '12 at 01:00