【问题标题】:SMTP Client 5.5.1 issueSMTP 客户端 5.5.1 问题
【发布时间】:2014-09-24 06:56:45
【问题描述】:

我有一个使用 C# 的简单 Web smtpclient 应用程序。

//store server name
string ServerName = "smtp.gmail.com";

// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(ServerName);
// Specify the e-mail sender. 
// Create a mailing address that includes a UTF8 character 
// in the display name.
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Port = 587;
MailAddress from = new MailAddress("youremailid",
    "nameprefix " + (char)0xD8 + " namesuffix",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("receipentemailid");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject. 
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback  
// method to identify this send operation. 
// For this example, the userToken is a string constant. 
string userState = "test message1";
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn't been sent yet, 
// then cancel the pending operation. 
if (answer.StartsWith("c") && mailSent == false)
{
    client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine("Goodbye.");

它给了我:

[testMessage1] System.Net.Mail.SmtpException:SMTP 服务器需要 安全连接或客户端未通过身份验证。服务器 响应是:5.5.1 需要身份验证。

1)- 有什么办法可以解决这个问题?

2)- 是否有任何限制以保持收件人与发件人相同的域服务器? 即两者都应该是 gmail.com 或 yahoo.com

【问题讨论】:

  • 您需要用户名和密码才能登录 SMTP 服务器吗?
  • 我怎么知道这个? @YuvalItzchakov
  • 检查来自 gmail 的文档。

标签: c# smtpclient


【解决方案1】:

您必须设置凭据:

client.Credentials = new NetworkCredential("username", "password");

【讨论】:

  • 服务器“smtp.gmail.com”已经被硬编码并传递给上面代码中的“SmtpClient client = new SmtpClient(ServerName)”
  • 我错过了。证件对吗?还有,你是在设置UseDefaultCredentials之后放的吗?
  • 是的,我用过:'client.UseDefaultCredentials=false; client.Credentials = new NetworkCredential("2014j.....", "*******");
  • 老实说,我不知道。稍后会做一些测试。
  • 它现在运行顺利,但我没有看到任何电子邮件收件箱((“receipentemailid”))。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 2010-12-10
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多