- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下程序使用“smtp.gmail.com:587”发送电子邮件
namespace TestMailServer
{
class Program
{
static void Main(string[] args)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myTest@gmail.com");
mail.To.Add("myTest2@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myTest@gmail.com", "myPassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Console.WriteLine("Send out");
}
}
}
myTest@gmail.com、myTest2@gmail.com 确实存在,myTest@gmail.com 的密码是myPassword。为什么会出现以下错误:
Unhandled Exception:System.Net.Mail.SmtpException: TheSMTP server requires a secureconnection or the client was notauthenticated. The server responsewas: 5.5.1 Authentication Required.Learn more at atSystem.Net.Mail.MailCommand.CheckResponse(SmtpStatusCodestatusCode, String response) atSystem.Net.Mail.MailCommand.Send(SmtpConnectionconn, Byte[] command, String from)
atSystem.Net.Mail.SmtpTransport.SendMail(MailAddresssender, MailAddressCollectionrecipients, String deliveryNotify,SmtpFailedRecipientException&exception) atSystem.Net.Mail.SmtpClient.Send(MailMessagemessage) atTestMailServer.Program.Main(String[]args) in D:\visual studio2010\Projects\TestMailServer\TestMailServer\Program.cs:line26 Press any key to continue . . .
最佳答案
我不确定是什么导致了您的问题。这是我一直用来通过 gmail 帐户成功发送电子邮件的一些代码:
const string from = "...";
var fromAddr = new MailAddress(from, "Bug Tracker");
var toAddr = new MailAddress("...@...", "...");
var client = new SmtpClient {
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Timeout = 30 * 1000,
Credentials = new NetworkCredential(fromAddr.Address, "...")
};
using (var msg = new MailMessage(fromAddr, toAddr)) {
msg.Subject = "...";
msg.Body = string.Format("username: {0}\nversion: {1}\n\n{2}", Environment.UserName, Assembly.GetExecutingAssembly().GetName().Version.ToString(3), cbtext);
client.Send(msg);
}
关于c# - 为什么我用google 'smtp'发不了邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5819065/
我是一名优秀的程序员,十分优秀!