0

When I use outlook, I am able to send test email to my gmail address, however, when I do it from a console application it triggers : "The server response was: 5.7.1 Unable to relay"

using System.Net.Mail;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage mail = new MailMessage("xxx@myCompany.com", "xxx@gmail.com");
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = "xxx.xxx.com";
            mail.Subject = "this is a test email.";
            mail.Body = "this is my test email body";
            client.Send(mail);
        }
    }
}

I verified that i have the correct client host through outlook. I also sent a test email to myself (from xx@mycompany to xx@mycompany) and that worked (although it sent it to the junk box). Why will it not let me send outgoing emails through this console app, but I can through the same address in outlook.

Jacob Alley
  • 767
  • 8
  • 34
  • check similar questions : http://stackoverflow.com/questions/3165721/mailbox-unavailable-the-server-response-was-5-7-1-unable-to-relay-for-abcxyz – Dipen Shah Oct 18 '16 at 14:52
  • yea i did, that doesnt seem like the same issue to me, despite the same error message. – Jacob Alley Oct 18 '16 at 14:55
  • im sending TO a gmail account, wouldnt i need the local port of the smtp server? – Jacob Alley Oct 18 '16 at 14:57
  • @JacobAlley How do you know the email is actually failing? You are able to send to yourself, so that indicates that the email is actually successfully being sent. Have you checked the spam folder in the recipient account? – user1666620 Oct 18 '16 at 14:59
  • because the console app is throwing an exception and crashing the program, something it wasnt doing when i was able to send it to myself. – Jacob Alley Oct 18 '16 at 15:00
  • Ah, that seems to indicate an issue with the SMTP itself, not the code. try this? https://blogs.technet.microsoft.com/jhoward/2005/10/11/resolving-smtp-error-550-5-7-1-unable-to-relay-for-userdomain-com-error-0x800ccc79/ – user1666620 Oct 18 '16 at 15:01
  • @Equalsk this is a problem sending the email to another domain from exchange, not gmail. – user1666620 Oct 18 '16 at 15:03
  • @JacobAlley this might be better asked on ServerFault - check this thread http://serverfault.com/questions/476979/exchange-connector-wont-send-to-external-domains – user1666620 Oct 18 '16 at 15:10
  • Thanks man, i'll check it out – Jacob Alley Oct 18 '16 at 15:12

1 Answers1

1

I'm pretty sure that if you have client.UseDefaultCredentials = false;, you need to set the credentials. At least that is what I do:

 client.UseDefaultCredentials = false;

 client.Credentials = new System.Net.NetworkCredential(someusername, somepassword);

edit: I should clarify, client.UseDefaultCredentials = false;, does not necessarily mean you need credentials listed, but if you are trying to send to an external domain (gmail.com), then your SMTP server will most likely require some type of SMTP Auth.

Mike_G
  • 16,237
  • 14
  • 70
  • 101
  • It depends - you don't necessarily need to provide credentials if the SMTP accepts anonymous connections. Besides, the emails send successfully to itself (indicating the connection to the SMTP is not the problem), just not to other domains. This seems to be a permissions error on the SMTP. – user1666620 Oct 18 '16 at 15:06
  • All emails sent to themselves will succeed if they are hosted on the same domain/server. OP is trying to send to gmail. I bet if he deleted his creds from outlook, and tried to send to gmail, he would get the same error. – Mike_G Oct 18 '16 at 15:10
  • So you agree this answer probably won't help. – user1666620 Oct 18 '16 at 15:12
  • No I don't agree, because his issue is when he sends to an external domain in the console app. – Mike_G Oct 18 '16 at 15:14
  • he was right! lol thanks guys, i appreciate both of your help tremendously – Jacob Alley Oct 18 '16 at 15:20