Search code examples
emailsmtpclient

.NET - Mail server doesn't send mail through SmtpClient.Send


I wrote a single console application (just a part of a site code, but it must work apart too, and it has the same fault result as inside the site) (C#):

MailMessage message = new MailMessage("login@ourDomenInPunycode", "toMail")
{
    Subject = "Hello",
    Body = "Hello world"
};

SmtpClient client = new SmtpClient();
client.Host = "ourIP";
client.Credentials = new System.Net.NetworkCredential("login@ourDomenInPunycode", "ourPassword");
client.Port = 25;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message); 

So, isn't to send e-mail should be trivial? But wherever I send mail from local machine through our mail server (just running this console application), the following exception appears:

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Relaying to denied (authentication required)

If I change the "login@ourDomenInPunycode" data to my own mailbox (at gmail or something else - no matter), all works fine. It also not depend from "toMail" address.

So, what could be wrong with our mail server? Any special settings? We use Windows Server 2008 virtualized inside another Windows Server 2008 and Kerio Connect 7 as mail server at virtual Windows Server 2008. All other mail programs like Outlook works well with sending e-mails from our mail server.

All articles which I read in Internet about SmtpClient's settings have only these (above) trivial settings and code, nothing special.

UPDATE

I done some fixes in text above.

Here is a part of log of our mail server when I tried to send mail through console application launched from the mail server virtual PC ("mail.ourDomen.local" related to "ourIP" above):

Task 215 handler BEGIN
Task 215 handler starting
SMTP server session begin; client connected from mail.ourDomen.local:49399
Sent SMTP greeting to mail.ourDomen.local:49399
Command EHLO OurMailServer
Sent reply to EHLO: 250 mail.ourDomenInPunycode ...
Command MAIL FROM:<login@ourDomenInPunycode>
Sent reply to MAIL: 250 2.1.0 Sender <login@ourDomenInPunycode> ok
Command RCPT TO:<toMail>
Sent reply to RCPT: 550 5.7.1 Relaying to <toMail> denied
Connection to SMTP server mail.ourDomen.local lost: connection closed by remote host.
SMTP server session end
Task 215 handler END

"Sent reply to RCPT: 550 5.7.1 Relaying to denied" - Why this happened?


Solution

  • Well, we use this description

    https://kb.kerio.com/article/550-571-relaying-to-email%40addresscom-denied-authentication-required-411.html .

    Although we know about this settings, but we tangled with our virtual machines. We have a virtual machine for the web server and another one for the mail server. Permissions were configured for the mail server virtual machine only in the Kerio Connect, not for the web server. We just added permission for the virtual machine of the web server and the mail is sent normally.

    And the "ourIP" in the

    SmtpClient client = new SmtpClient();
    client.Host = "ourIP";
    

    is the IP of our virtual machine of the mail server. No settings of IP of the web server virtual machine in the SmtpClient object.