SMTP客户端5.5.1问题

时间:2014-09-24 06:56:46

标签: c# smtpclient

我有一个使用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) - 将receipent与发件人保持同一域服务器是否有任何限制?     即两者都应该是gmail.com或yahoo.com

1 个答案:

答案 0 :(得分:0)

您必须设置凭据:

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