Hi i am trying to send an email via C# and having a little trouble with error code:
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for =@p0

I have had a good look round and it seems it is a problem with authentication... So i set up a user called PurchaseOrder on our exchange server and enabled the account. but i am still getting this error

SmtpClient smtp = new SmtpClient("");
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("PurchaseOrder", "qwer123!");

            catch (Exception e)
                System.Windows.Forms.MessageBox.Show("Email not sent correctly: {0}", e.ToString());

Anyone got any ideas?

6 Years
Discussion Span
Last Post by jammiedude

Two things;
Check that your Email variable is set correctly.
Add smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

Another thing is you should catch the specific exceptions being thrown, rather than the generic Exception. This will give you more information on why your code is failing:

SmtpClient smtp = new SmtpClient("");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("PurchaseOrder", "qwer123!");

try {
} catch (ArgumentNullException e) {
    MessageBox.Show("Message is null");
} catch (InvalidOperationException e) {
    MessageBox.Show("Invalid Operation - {0} - {1}", e.ToString(), e.InnerException.ToString());
} catch (ObjectDisposedExeption e) {
    MessageBox.Show("SMTP object was disposed");
} catch (SmtpException e) {
    MessageBox.Show("SMTP issue - {0} - {1}", e.ToString(), e.InnerException.ToString());
} catch (SmtpFailedRecipientsException e) {
    MessageBox.Show("Message could not be deliverd - {0}", e.ToString());
} catch (Exception e) {
    MessageBox.Show("System failed - {0} - {1}", e.ToString(), e.InnerException.ToString());

Edited by Momerath: n/a


Ok i tried breaking the error code down and as i step through it jumps straight to smtpexception but gives an additional error, Object reference not set to an instance of an object. i think e.innerexception is null thats why i am seeing this error.
also Email variable is:

MailMessage Email = new MailMessage();

The email does actually hit the recipients box its just my program crashes with the error as stated in beginning post.


You've failed to initialize a needed part of the MailMessage. Obviously, you have the ToAddresses down or the recipients would not get it. Without know how you are building the MailMessage, my suggestion is to take a look at the classes where you are adding the body or attachments or even CC. I suspect the problem is that one of the classes you are using to build the MailMessage is null.



Thank you so much! what you said eventually led me to a incorrect send spelling in the to address and it was correct in the cc field because it was hard coded where the to was pulled in from db.

I've not long started to learn C# and programming so i do apologise if it seems like a stupid mistake but at least im learning from it!

Thanks all for your posts!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.