这是我的问题:我写了以下程序来测试我是否可以发送电子邮件:
class Program
{
static void Main(string[] args)
{
try {
Console.WriteLine("Mail To");
MailAddress to = new MailAddress("myemail@gmail.com");
Console.WriteLine("Mail From");
MailAddress from = new MailAddress("me@businessdomain.it");
MailMessage mail = new MailMessage(from, to);
Console.WriteLine("Subject");
mail.Subject = "test";
Console.WriteLine("Your Message");
mail.Body = "test";
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.domain.it";
smtp.Port = 25;
smtp.Credentials = new NetworkCredential(
"username", "password");
smtp.EnableSsl = false;
Console.WriteLine("Sending email...");
smtp.Send(mail);
}catch(Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
凭据是正确的(我使用telnet测试它们,outlook和android中的应用程序k9mail正常工作),如果我把gmail smtp设置,该程序可以工作。 我真的无法理解这个错误的原因。
使用wireshark我发现了正在发生的事情:
S:220 server1.business.it SMTP服务器就绪
C:EHLO pc14
S:250 server1.stargatenet.it你好[87.28.219.65] | 250 PIPELINING | 250 SIZE 25000000 | 250 8BITMIME | 250 BINARYMIME | 250 CHUNKING | 250 AUTH LOGIN CRAM-MD5 DIGEST-MD5 | 250好的
C:AUTH登录用户:UsernameBase64
S:334 VXNlcm5hbWU6
C:通过:PasswordBase64
S:334 UGFzc3dvcmQ6
似乎程序在询问时没有输入凭据。怎么可能?
答案 0 :(得分:1)
此链接拯救了我的生命:link
这里我遇到的问题很好描述:即使用AUTH LOGIN传递用户名,服务器也会再次响应AUTH_LOGIN_Username_Challenge。
仅在使用System.Net.Mail时才会出现此问题。链接建议了可能的解决方案:
1)使用CDOSYS(不通过AUTH登录发送用户名)
2)使用System.Web.Mail(不通过AUTH登录发送用户名)
3)联系SMTP服务器所有者并让他们修复服务器。
不幸的是我无法成为SMTP服务器所有者,所以我不得不使用System.Web.Mail。我知道它已被弃用但不幸的是在这样的情况下,没有其他选择IMO。
这是我的工作代码:
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
msg.Body = message.Body;
string smtpServer = "mail.business.it";
string userName = "username";
string password = "password";
int cdoBasic = 1;
int cdoSendUsingPort = 2;
if (userName.Length > 0)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
msg.To = message.Destination;
msg.From = "me@domain.it";
msg.Subject = message.Subject;
msg.BodyFormat = MailFormat.Html;//System.Text.Encoding.UTF8;
SmtpMail.SmtpServer = smtpServer;
SmtpMail.Send(msg);
这个answer帮助我使用System.Web.Mail。
答案 1 :(得分:0)
虽然我遇到了同样的问题,但我找到了解决问题的方法here
简而言之,您必须使用
smtp.UseDefaultCredentials = false;
之后
smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email.username"].ToString(),
ConfigurationManager.AppSettings["email.password"].ToString());
如此简短的总结,应该看起来像这样:
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["email.smtp"].ToString();
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["email.port"]);
smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email.username"].ToString(),
ConfigurationManager.AppSettings["email.password"].ToString());
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["email.enablessl"]);
Console.WriteLine("Sending email..." + (i+1));
smtp.Send(mail);
答案 2 :(得分:0)
对于任何有兴趣的人。我今天有这个问题。并发现我的解决方案是订单。
SMTPClient client = new SMTPClient();
client.UseDefaultCredentials = false; // first after declare and needs to be false.
然后:
client.Credentials = new System.Net.NetworkCredential(sMTPSettings.SMTPLogin, sMTPSettings.SMTPPassword);
然后休息。
希望它对某人有帮助