SMTP客户端5.5.1问题

本文关键字:问题 SMTP 客户端 | 更新日期: 2023-09-27 18:24:06

我有一个使用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客户端5.5.1问题

您必须设置凭据:

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