1

The below code is working fine from Windows Form, but not working from Windows Services. The service is running on Windows XP.

I have tried changing the log on user, but did not work.

Error: Transaction failed. the server response was 5.7.1 client host rejected access denied

private void SendEmailToHO()
{
    try
    {
        int mailSentSuccessfully = 0;

        MailAddress to = new MailAddress(mailTo);
        MailAddress from = new MailAddress(mailFrom);

        using (MailMessage mail = new MailMessage(from.Address, to.Address))
        {

            int attachmentCount = 0;

            try
            {
                foreach (string fileName in fileEntries)
                {
                    Attachment attachment = new Attachment(fileName);
                    mail.Attachments.Add(attachment);
                    attachmentCount++;
                }

                SmtpClient client = new SmtpClient(mailHost, port);
                if (enableSSL == "Y")
                {
                    client.EnableSsl = true;
                }
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(mailUser, mailPassword);

                mail.Subject = "Email Subject " + clientID;
                mail.Body = "Attached please find the files: " + clientIDTitle;

                if (attachmentCount > 0)
                {
                    //
                    client.Send(mail);

                    //if no error, this code will work.
                    mailSentSuccessfully = 1;

                    new MyApp.LogWriter("Sent mail to " + to.Address + ", \nAttachment count = " + attachmentCount);
                }
                else
                {
                    new MyApp.LogWriter("Attachment count = " + attachmentCount);
                }
            }
            catch (Exception ex)
            {
                new MyApp.LogWriter("Send mail failed. Cause: " + ex.Message
                                          + "\n Inner Exception: " + ex.InnerException);
            }
        }
    }
    catch (System.Exception ex)
    {
        new BTCClient.LogWriter("Email Error '" +
                           ex.Message + "'");
    }
}
live2
  • 3,771
  • 2
  • 37
  • 46
Rauf
  • 12,326
  • 20
  • 77
  • 126
  • 1
    Are you sure you are allowed to send emails through that SMTP server from the IP address where this code is running? – DavidG Apr 19 '18 at 09:40
  • @DavidG Yes. But this kind of code works from Windows Form. I think this is related with file Permission issues ??? – Rauf Apr 19 '18 at 09:42
  • That message is from the relaying server and it clearly states it's rejecting your mail. This could be down to numerous things, for example, Credentials, your sending IP (Blacklisted), security implemented on the recipient SMTP server (Do you need a valid SPF, valid PTR (For DMARC) etc...). So many things it could be. I recommend you check the logs of the SMTP server your attempting to relay. – Kitson88 Apr 19 '18 at 10:03
  • @Rauf Check one of my answers out for troubleshooting SMTP: https://stackoverflow.com/questions/39714281/mail-of-php-dns-request-timed-out-and-fqdn/39722704#39722704 – Kitson88 Apr 19 '18 at 10:06
  • @Kitson88 I will check. But I'm curious to know why the code works from Windows Form ? – Rauf Apr 19 '18 at 10:07
  • @Rauf Same lib or same code? Who's your SMTP relaying provider? – Kitson88 Apr 19 '18 at 10:09

1 Answers1

0

I was facing the same issue, To resolve this issue you need to contact your email server's service provider and tell them to give access to your server IP.