[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-06-30
8
Medium Priority
?
638 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Rick Fee
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

One-stop solution for Exchange Administrators to address all MS Exchange Server issues, which is known by the name of Stellar Exchange Toolkit.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
To show how to create a transport rule 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 >> Rules tab.:  To cr…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Suggested Courses

650 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