我一直试图通过C#发送电子邮件.我用谷歌搜索了各种各样的例子,并从每个人和最有可能使用的标准代码中获取了一些零碎的东西.
string to = "receiver@domain.com";
string from = "sender@domain.com";
string subject = "Hello World!";
string body = "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
client.Credentials = new NetworkCredential("test@domain.com", "password");
client.Send(message);
Run Code Online (Sandbox Code Playgroud)
但是,我不断收到错误说明
System.Net.Mail.SmtpException:邮箱不可用.服务器响应是:拒绝访问 - 无效的HELO名称(请参阅RFC2821 4.1.1.1)
那么,我现在该怎么办?SmtpClient应该是特殊的,只适用于特定的SMTP服务器吗?
您的用户名/密码对似乎未成功通过SMTP服务器进行身份验证.
我想,我发现这里有什么问题.我已在下面更正了您的版本.
string to = "receiver@domain.com";
//It seems, your mail server demands to use the same email-id in SENDER as with which you're authenticating.
//string from = "sender@domain.com";
string from = "test@domain.com";
string subject = "Hello World!";
string body = "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("test@domain.com", "password");
client.Send(message);
Run Code Online (Sandbox Code Playgroud)