Solved

Why does my mail script throw an 'unable to relay' exception on Exchange?

Posted on 2009-06-30
8
598 Views
Last Modified: 2012-05-07
I have a form on our website that handles emails.  Instead of having traditional mailto links, we have linkbuttons that direct to a form that sends the email.  It's a spam thing.  Anyway, this all worked beautifully before we deployed our new Exchange mail server last week.  Now when someone enters an address OUTSIDE our domain in the CC: field, the mail server says "550 5.7.1 Unable to relay..."  All I did was change the old mail server to the new one in the SmtpMail.SmtpServer setting.

The code is below.  I have commented out the try...catch block so that I can see the error message instead of my error page.  If you want to try the form, go here: http://www.co.frederick.va.us/apps/email_form/email_form_2.aspx?user=webmastr.  

What do I have to do to make this work with our Exchange server?
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.Configuration;

using System.Data.SqlClient;

using System.Web.Mail;
 

namespace email_form

{

    public partial class _Default : System.Web.UI.Page

    {
 

        // Global variables.

        string strDomain;

        string strResult;
 

        protected void Page_Load(object sender, EventArgs e)

        {

            try

            {

                string strEmailUser = Request["user"].ToString();

                if (strEmailUser != null)

                {

                    if (strEmailUser.Contains("@"))

                    {

                        strResult = strEmailUser;

                    }

                    else

                    {

                        strDomain = "@co.frederick.va.us";

                        strResult = strEmailUser + strDomain;

                        // lblTestSession.Text = strResult;

                    }

                }

                if (strEmailUser == "")

                {

                    // lblTestSession.Text = "No data!";

                    Response.Redirect("problem_variable.aspx");

                }

            }

            catch (Exception ex1)

            {

                Response.Redirect("problem_variable.aspx");

            }

        }
 

        protected void btnSend_Click(object sender, EventArgs e)

        {

            MailMessage objMail0 = new MailMessage();
 

            // Initial setup.

            objMail0.To = strResult;

            objMail0.Cc = Server.HtmlEncode(txtCC.Text);

            string strCC = txtCC.Text;

            objMail0.From = Server.HtmlEncode(txtSenderEmail.Text);

            string strFromEmail = Server.HtmlEncode(txtSenderEmail.Text);

            string strFromName = Server.HtmlEncode(txtSender.Text);

            objMail0.Headers.Add("Reply-To", Server.HtmlEncode(txtSenderEmail.Text));

            objMail0.BodyFormat = MailFormat.Text;

            objMail0.Priority = MailPriority.Normal;

            objMail0.Subject = Server.HtmlEncode(txtSubject.Text);

            string strToday = DateTime.Now.ToString();

            string strIPAddress = Request.ServerVariables["remote_addr"];

            //System.Net.IPHostEntry host = new System.Net.IPHostEntry();

            //host = System.Net.Dns.GetHostByAddress(Request.ServerVariables["REMOTE_HOST"]);

            //string strMachineName = host.HostName;

            string strMessage = Server.HtmlEncode(txtMessage.Text);
 

            //Format the email.

            objMail0.Body = "**This message was sent via the website email form.**" +

                            "\r\n\r\nSender: " + strFromName +

                            "\r\nEmail: " + strFromEmail +

                            "\r\nIP address: " + strIPAddress + 

                            //"\r\nHost: " + strMachineName + 

                            "\r\nDate: " + strToday + 

                            "\r\nCC: " + strCC +

                            "\r\n\r\nMessage: " + "\r\n" + strMessage

                            ;
 

            //Send the email. 

            SmtpMail.SmtpServer = "fcmail.co.frederick.va.us";

            //try

            //{

                SmtpMail.Send(objMail0);

            //}

           // catch (Exception ex)

            //{

            //    Response.Redirect("problem.aspx");

            //}
 

            Session.Remove("emailUser");

            Response.Redirect("thanks.aspx");

            

        }

    }

}

Open in new window

0
Comment
Question by:mrcoulson
  • 5
  • 2
8 Comments
 

Author Comment

by:mrcoulson
ID: 24744771
PS Please tell me the answer does not involve rewriting the entire thing with System.Net.Mail!

Jeremy
0
 
LVL 20

Expert Comment

by:EndureKona
ID: 24745122
Sounds like all you have to do is setup email relay for the IP of your webmail server.    Is this Exchange 2007?     Be careful with this if the webserver is external.   You don't want to allow spammers to relay off you.

How to setup relaying in Exchange 2007:
http://msexchangeteam.com/archive/2006/12/28/432013.aspx
0
 
LVL 12

Expert Comment

by:ryan80
ID: 24745140
In the relaying, you can set it so only specific IP's are allowed to relay, so you should be able to just add the IP address of your web server.
0
 

Author Comment

by:mrcoulson
ID: 24745291
Actually, guys, it was just SMTP authentication!  The network administrator is quite a hawk, so I assumed he had locked it down well.  I just needed to create an account that can authenticate to send these messages.

Jeremy
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mrcoulson
ID: 24745296
By the way, the final solution is below.
0
 

Accepted Solution

by:
mrcoulson earned 0 total points
ID: 24745299

objMail0.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");	//basic authentication

            objMail0.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "user"); //set your username here

            objMail0.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "pass");	//set your password here

            SmtpMail.SmtpServer = "fcmail.co.frederick.va.us";

Open in new window

0
 
LVL 12

Expert Comment

by:ryan80
ID: 24745311
yup, that would definitly help.
0
 

Author Comment

by:mrcoulson
ID: 24745346
Yeah, Exchange is brand new to me.  Sorry for wasting everyone's time with an easy one!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now