Link to home
Start Free TrialLog in
Avatar of kathysmith
kathysmith

asked on

ASP.NET send email - "Mailbox unavailable. The server response was: 5.7.1 Unable to relay" error

Hei!

I've implemented an ASP.NET (C#) application which has a send email functionality. The code works well when running from Visual Studio IDE against my localhost. When running on the staging web server, I'm getting the following error message:

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for someName@domainName.com

(where someName@domainName.com is the email address in the "to"-field)

Implementation details:
web.config:
<mailSettings>
  <smtp>
    <network host="hostName" port="25"/>
  </smtp>
</mailSettings>

Open in new window


simplified code sequence (which also gives the error mentioned above):
public static void SendEmail(string server)
{
string to = "jane@contoso.com";
string from = "ben@contoso.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Some subject";
message.Body = @"Some body";
SmtpClient client = new SmtpClient();
client.Send(message);
}

Open in new window


Other details:
- the hostName and port value in web.config are the values I've got from the system administrator (SA)
- there is no Default SMTP Virtual server on our IIS (which is an IIS6)
- SA does not want to install a Default SMTP Virtual server on IIS
- SA wants that the emails sendt by web applications use the hostName and port value in the web.config
- SA says I don't need a special username and password for seding email

What I tried:
- added deliveryMethod="Network": <smtp deliveryMethod="Network">
- changed to: <network host="hostName" port="25" defaultCredentials="true"/>
None of these helped.

QUESTION
- what settings/code do I need in order to solve this problem?

I've seen the error message quite a lot on Internet, but I haven't found the right solution for my scenario.
Avatar of Jini Jose
Jini Jose
Flag of India image

you need smtpcredentials to send mail

refer the below code

private static void SendThroughSMTP(MailMessage msg)
    {
        SmtpClient Smtp = new SmtpClient();
        System.Net.NetworkCredential SmtpUser = new System.Net.NetworkCredential();
        //--Build Message

        //'--Define AuthenticatedUser;
        SmtpUser.UserName = "SMTPUser";
        SmtpUser.Password = "SMTPPassword";
        SmtpUser.Domain = "SMTPServer";
        //'--Send Message
        Smtp.UseDefaultCredentials = false;
        Smtp.Credentials = SmtpUser;
        Smtp.Host = "SMTPServer";
        Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        Smtp.Send(msg);
    }

Open in new window

Avatar of kathysmith
kathysmith

ASKER

NOTE: the values in the "to" and "from" fields do not contain yahoo, gmail or hotmail.
that is great !!. if your server cannot send mails to yahoo, gmail or hotmail then it is interesting that who else can you able to send. i think there is nothing remaining.
I think it is only "from" fields do not contain the listed domains.
@gmailjini: thanx for quick answer. But, as I wrote: "SA says I don't need a special username and password for seding email". So?
I've tested sending of email with values as someName@company.com in the "to" and "from" fields. But it doesn't work.
ASKER CERTIFIED SOLUTION
Avatar of Jini Jose
Jini Jose
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ultrawebsites: before I posted the question I tried sending with different "to" and "from" addresses, as you suggested, but it didn't help.

The conclusion: our SA should enable relaying on the SMTP server, eventually assign credentials that I can use further in my code.

Thank you both for answers. I'll share the points between you two, since both of you helped me.

Hi there
SMTP can certainly be a pain to configure. FYI, specifically, whilst 'to' and 'from' should typically be different, in the instance I'm thinking of, it is important that the 'from' address is the same domain as the website. ie. the website SMTP settings et al only permit you to send email from the domain name that the code is running from, as per security settings.
HTH
Cheers
Matt
Hi everybody!

My problem has been solved, and the solution has been worked well since. The solution is:
1. No "Default SMTP Virtual server" has been installed on our IIS (we have IIS6 on test environment and IIS7 on production).
2. In web.config, the only thing you need is:
3. The method that performs the sending is the same as I mentioned in my question.
4. The system administrator has configure the MS Exchange server. Unfortunately, I don't know how he did it.
After all these 4 steps, everything works.

<mailSettings>
  <smtp>
    <network host="hostName" port="25" defaultCredentials="true"/>
  </smtp>
</mailSettings>

Open in new window