问题描述
错误:信箱不可用.服务器响应是:访问被拒绝 - HELO 名称无效(请参阅 RFC2821 4.1.1.1)
ERROR: Mailbox unavailable. The server response was: Access denied - Invalid HELO name (See RFC2821 4.1.1.1)
说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
异常详细信息:System.Net.Mail.SmtpException:邮箱不可用.服务器响应是:访问被拒绝 - HELO 名称无效(请参阅 RFC2821 4.1.1.1)
Exception Details: System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: Access denied - Invalid HELO name (See RFC2821 4.1.1.1)
源错误第 61 行:
Line 59:
Line 60: SmtpClient smtp = new SmtpClient(SMTPServer, SMTPPort);
Line 61: smtp.Credentials = new NetworkCredential(SMTPUserId, SMTPPassword);
Line 62: smtp.Send(mail);
Line 63: }
源文件:WebsitesClientReportsContactUs.aspx.cs 行:61
Source File: WebsitesClientReportsContactUs.aspx.cs Line: 61
============================================================================
=========================================================================
我的源代码:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string name = txtFirstName.Text + " " + txtLastName.Text;
string clientID = Session["CustomerID"].ToString();
string message = txtMessage.Text;
string email = txtEmail.Text;
string company = txtCompany.Text;
string phone = txtPhone.Text;
SMTPMailHelper.SendMail(email, "myemail@domain.com", "Contact from Client: " + clientID + ": " + name + " " + company, message);
}
我遗漏了一些无关紧要的脚本,例如条件语句.这是其余的:
I LEFT OUT SOME SCRIPT THAT DID'T MATTER SUCH AS CONDITIONAL STATEMENTS. AND THIS IS THE REST OF IT:
public class SMTPMailHelper
{
const string SMTPServer = "mail.mydomain.com";
const string SMTPUserId = "myemail@mydomain.com";
const string SMTPPassword = "MyPasswordHidden";
const int SMTPPort = 25;
public static void SendMail(string sendFrom, string sendTo, string subject, string body)
{
MailAddress fromAddress = new MailAddress(sendFrom);
MailAddress toAddress = new MailAddress(sendTo);
MailMessage mail = new MailMessage();
mail.From = fromAddress;
mail.To.Add(toAddress);
mail.Subject = subject;
if (body.ToLower().Contains("<html>"))
{
mail.IsBodyHtml = true;
}
mail.Body = body;
SmtpClient smtp = new SmtpClient(SMTPServer, SMTPPort);
smtp.Credentials = new NetworkCredential(SMTPUserId, SMTPPassword);
smtp.Send(mail);
}
}
推荐答案
您遇到的问题是 SmtpClient 发出带有主机名的 HELO
或 EHLO
命令您的服务器拒绝 SMTP 的/domain 字符串.
The problem you are having is that the SmtpClient is issuing a HELO
or EHLO
command with a hostname/domain string that your server is SMTP rejecting.
我显然不知道微软 SmtpClient 实现的内部逻辑,但我怀疑你可能在 NAT 后面,这意味着 SmtpClient 无法解析你机器的全球可见的完全限定域名.它只能在内部网络"上解析您机器的 FQDN(实际上,它甚至可能无法将其解析为实际的 FQDN,它可能必须解决内部网络 IP 地址).
I obviously cannot know the inner logic of Microsoft's SmtpClient implementation, but I suspect that you are probably behind a NAT which means that the SmtpClient cannot resolve the world-viewable fully qualified domain name of your machine. It can only resolve the FQDN of your machine on your "internal network" (and realistically, it probably can't even resolve it to an actual FQDN, it probably has to settle for the internal network IP address).
所以... SmtpClient 可能正在发送类似 EHLO [192.168.1.100]
的内容(或任何您的本地 IP)并且服务器拒绝它,因为那不是它的 IP 地址接收数据包.
So... SmtpClient is probably sending something like EHLO [192.168.1.100]
(or whatever your local IP is) and the server is rejecting it because that is not the IP address that it is receiving packets from.
您需要做的是通过访问 http://www 之类的网站找到您的外部 IP 地址.whatismyip.com/ 或使用像 http://www.displaymyhostname.com/这样的网站检测到的主机名.
What you'll need to do is find your external IP address by visiting a site like http://www.whatismyip.com/ or using the hostname detected by a site like http://www.displaymyhostname.com/.
为了说明我在说什么,这里有一个小演示程序:
To illustrate what I am talking about, here's a little demo program:
using System;
using System.Net;
using System.Linq;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Net.NetworkInformation;
namespace LocalHostName
{
class Program
{
static void Main (string[] args)
{
var ipAddresses = Dns.GetHostAddresses ("www.google.com");
Socket socket;
for (int i = 0; i < ipAddresses.Length; i++) {
socket = new Socket (ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try {
socket.Connect (ipAddresses[i], 80);
Console.WriteLine ("LocalEndPoint: {0}", socket.LocalEndPoint);
break;
} catch {
socket.Dispose ();
socket = null;
if (i + 1 == ipAddresses.Length)
throw;
}
}
Console.ReadLine ();
}
}
}
它打印出的任何 IP 地址都可能是 SmtpClient 在其 HELO
/EHLO
命令中使用的那个.
Whatever IP address it prints out is likely to be the one that SmtpClient is using in its HELO
/EHLO
command.
坏消息是 System.Net.Mail.SmtpClient 无法解决这个问题.
The bad news is that there's no way to work around this problem with System.Net.Mail.SmtpClient.
好消息是我节省了 15% 的汽车保险费……呃,我的意思是,我已经编写了自己的电子邮件库,名为 MimeKit 和 MailKit 可以 解决这个问题.
The good news is that I saved 15% on my car insurance... er, I mean, I've written my own email libraries called MimeKit and MailKit that can work around this problem.
还有一些好消息,您可以轻松地将 System.Net.Mail.MailMessage 对象转换为 MimeKit.MimeMessage 对象(但显然,最好完全切换到 MimeKit 和 MailKit).
Some more good news is that you can easily cast your System.Net.Mail.MailMessage object into a MimeKit.MimeMessage object (although, obviously, it would be better to switch completely over to MimeKit and MailKit).
var mimeMessage = (MimeMessage) mailMessage;
这是使用 MimeKit 和 MailKit 重写的 SMTPMailHelper 类的样子:
Here's what your re-written SMTPMailHelper class would look like using MimeKit and MailKit:
using System;
using MimeKit;
using MailKit.Net.Smtp;
public class SMTPMailHelper
{
const string SMTPServer = "mail.mydomain.com";
const string SMTPUserId = "myemail@mydomain.com";
const string SMTPPassword = "MyPasswordHidden";
const int SMTPPort = 25;
public static void SendMail(string sendFrom, string sendTo, string subject, string body)
{
MimeMessage message = new MimeMessage ();
TextPart text;
message.From.Add (new MailboxAddress ("", sendFrom));
message.To.Add (new MailboxAdress ("", sendTo));
message.Subject = subject;
if (body.ToLower ().Contains ("<html>"))
text = new TextPart ("html") { Text = text };
else
text = new TextPart ("plain") { Text = text };
message.Body = text;
using (var smtp = new SmtpClient ()) {
// use the IP address gotten from http://www.whatismyip.com/
// or the hostname gotten from http://www.displaymyhostname.com/
smtp.LocalDomain = "c-24-71-150-91.hsd1.ga.comcast.net";
smtp.Connect (SMTPServer, SMTPPort, false);
smtp.Authenticate (SMTPUserId, SMTPPassword);
smtp.Send (message);
smtp.Disconnect (true);
}
}
}
重要的一点是您将 SmtpClient 的 LocalDomain 属性设置为的字符串.
The important bit is the string that you set the LocalDomain property of the SmtpClient to.
这篇关于收到 ASP 错误:邮箱不可用.服务器响应是:访问被拒绝 - HELO 名称无效(请参阅 RFC2821 4.1.1.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!