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);
}
}
}
}