-1

I am developing a C# application to send mail using the SMTP server of our company mail. The following is the code.

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("10.203.195.48");

mail.From = new MailAddress("");
mail.To.Add("");
mail.Subject = "filename";
mail.Body = "Report";

SmtpServer.Host = "ip address fo smtp mail server.";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("", "");

SmtpServer.Send(mail);

But I get this error:

mailbox unavailable.unable to relay.The system doesn't have internet connection.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Are you really passing in blank values for `mail.To.Add` and `System.Net.NetworkCredential`? The error message(s?) are pretty self-explanatory. `Unable to relay` - by default most corporate mail servers turn off relaying so they can't be used for sending out spam, for example. – Tim Apr 20 '13 at 07:30
  • Take a look at this thread - [`Error: Mailbox unavailable. The server response was 5.7.1 Unable to relay for (email)`](http://forums.devshed.com/net-development-87/error-mailbox-unavailable-the-server-response-was-5-7-1t-315971.html) – Tim Apr 20 '13 at 07:32

2 Answers2

1

The following code is for Gmail SMTP using port 587. You just change your port and SMTP.

Add Namespace

using system.net
MailMessage MyMailMessage = new MailMessage();
MyMailMessage.From = new MailAddress("emailid");
MyMailMessage.To.Add("To");
MyMailMessage.Subject = "Feedback Form";
MyMailMessage.Body = "This is the test message";
MyMailMessage.IsBodyHtml = true;

SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
SMTPServer.Port = 587;
SMTPServer.Credentials = new System.Net.NetworkCredential("Username","password");
SMTPServer.EnableSsl = true;

try
{
    SMTPServer.Send(MyMailMessage);
}

catch (Exception ex)
{
    ex.message("error");
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PoliDev
  • 1,408
  • 9
  • 24
  • 45
1

From Error: Mailbox unavailable. The server response was 5.7.1 Unable to relay for (email):

Generally it occurs when you have a mail server (e.g. mailserver.com) from one domain, and the addresses are from other domains. Either the From or the To address need to belong to the domain (myname@mailserver.com or yourname@mailserver.com). If neither of the addresses belong to a domain that the mail server 'owns', then you are relaying, which is a spam technique and is generally not allowed nowadays."

See also blog post Mailbox unavailable. The server response was: 5.7.1 Unable to relay sendername.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234