I've got a simple mail utility that is supposed to send an email through an Exchange 2007 server (which is installed on Windows Server 2008 R2 64bit) and it won't work, giving the following error message at the commandline: "Mailbox unavailable. The server response was: 5.7.1 Unable to relay".
I've been told I need to authenticate to the server but evidently I'm not doing it correctly. Any suggestions?
My code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace SendMail
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpClient smtpClient = new SmtpClient("x.x.x.x", 25);
            NetworkCredential basicCredential = new NetworkCredential("username", "password", "domain");
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("user@domain.com");
            smtpClient.Host = "x.x.x.x";
            
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = basicCredential;
            message.From = fromAddress;
            message.Subject = "test message";
            message.Body = "test message";
            message.To.Add("user@domain.com");            
            try
            {
                smtpClient.Send(message);
                Console.WriteLine("Message sent successfully");
            }
            catch (Exception ex)
            {    
                //Error, could not send the message
                Console.WriteLine(ex.Message);
            }
        }
    }
}

I've tried your code on my office network and it works fine.

Are you sure you are entering the correct information?

SmtpClient smtpClient = new SmtpClient("CLIENTIPADDRESSHERE", 25);
            NetworkCredential basicCredential = new NetworkCredential("SENDERUSERNAME", "SENDERPASSWORD", "TOUSNET");
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("FROMEMAILADDRESS");
            smtpClient.Host = "MAIL.YOURDOMAIN.COM";

            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = basicCredential;
            message.From = fromAddress;
            message.Subject = "test message";
            message.Body = "test message";
            message.To.Add("DESTINATION E-MAIL HERE");

I've tried it and it works fine. Perhaps your values aren't correct ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.