从 Windows 服务发送电子邮件:事务失败。服务器响应为 5.7.1 客户端主机拒绝访问被拒绝

问题描述 投票:0回答:1

以下代码在 Windows 窗体中运行良好,但在 Windows 服务中无法运行。 该服务在 Windows XP 上运行。

我尝试过更改登录用户,但没有成功。

错误:交易失败。服务器响应是 5.7.1 客户端主机 拒绝访问被拒绝

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 + "'");
    }
}
c# smtp windows-services
1个回答
0
投票

我遇到了同样的问题,要解决此问题,您需要联系您的电子邮件服务器的服务提供商并告诉他们授予对您的服务器 IP 的访问权限。

© www.soinside.com 2019 - 2024. All rights reserved.