Windows Service发送邮件失败:5.7.1客户端主机被拒绝访问
Hey there, let's figure out why your email code works perfectly in a WinForms app but throws that annoying 5.7.1 client host rejected access denied error when running in a Windows Service on Windows XP. Here are the most likely culprits and fixes to try:
1. SMTP Server IP/Whitelist Restrictions
WinForms apps run in your logged-in user's session, using the network IP associated with your user context. But Windows Services (especially when running under the default Local System account) might use a different network identity or public IP that your SMTP server hasn't whitelisted.
- Fix: Reach out to your SMTP server admin and ask to add the public IP of the machine running the service to their allowed list. Double-check that the service's network exit IP matches what the SMTP server sees (you can use a tool like
tracertor an external IP checker from the service's context to confirm).
2. Missing Proxy or Network Permissions
WinForms inherits your user's proxy settings automatically, but Windows Services (especially system accounts) don't. If your network uses a proxy to access external services, the service might be blocked from reaching the SMTP server.
- Fix: Explicitly set the proxy in your code if needed:
Also, make sure the service's running account has permission to access the SMTP server's port (usually 25, 465, or 587) through any network firewalls.using (SmtpClient smtp = new SmtpClient("your-smtp-server-address")) { smtp.Proxy = new WebProxy("your-proxy-server", 8080); // Replace with your proxy details smtp.Proxy.Credentials = new NetworkCredential("proxy-username", "proxy-password"); // Rest of your email sending code... }
3. Incorrect SMTP Authentication Settings
Even if you changed the service's login user, your code might be relying on Windows Integrated Authentication (which works for your WinForms user) but the service account doesn't have permission to authenticate with the SMTP server.
- Fix: Hardcode the SMTP credentials directly in your code (or store them securely) instead of relying on default authentication:
Keep in mind that Windows XP's .NET Framework versions (like 2.0/3.5) don't support TLS 1.1 or higher, so your SMTP server must enable TLS 1.0 for this to work.using (SmtpClient smtp = new SmtpClient("your-smtp-server-address")) { smtp.Credentials = new NetworkCredential("smtp-username", "smtp-password"); smtp.EnableSsl = true; // Critical if your server requires SSL // Note: Windows XP only supports TLS 1.0, so ensure your SMTP server allows this protocol // Rest of your email sending code... }
4. Windows XP Firewall Blocking Outbound Connections
Your WinForms app might have been granted firewall access when you first ran it, but the Windows Service (or the underlying svchost.exe process) is blocked from sending traffic to the SMTP port.
- Fix: Open Windows XP's Firewall settings, go to the "Exceptions" tab, and add a new rule allowing outbound connections to your SMTP server's port. You can also temporarily disable the firewall to test if this is the issue (remember to re-enable it afterward!).
Start with checking the SMTP whitelist and authentication settings first—those are the most common causes for this exact error in service environments.
内容的提问来源于stack exchange,提问作者Rauf

