【发布时间】: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