Solved

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

Posted on 2009-06-30
8
609 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lotus Notes ā€“ formerly IBM Notes ā€“ is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino isā€¦
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection ā€¦
This video discusses moving either the default database or any database to a new volume.

772 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