Reputation: 1317
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("[email protected]")
};
email.To.Add(new MailAddress("[email protected]"));
mailClient.Send(email);
Web.config Code
<system.net>
<mailSettings>
<smtp>
<network host="mail.myCompany.com" port="25" userName="[email protected]" 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.
Upvotes: 2
Views: 10239
Reputation: 10780
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
Upvotes: 0