为什么我使用Google SMTP无法发送电子邮件? [英] Why I use google smtp cannot send out email?
问题描述
您好
我有以下程序可以使用 smtp.gmail.com:587发送电子邮件。
Hi I have following program to send email by using "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。为什么会出现以下错误:
myTest@gmail.com, myTest2@gmail.com are really existing and myTest@gmail.com's password is myPassword. Why I got the following error:
未处理的异常:
System.Net.Mail.SmtpException:
SMTP服务器需要安全的
连接,或者客户端未通过
身份验证。服务器响应
是:5.5.1需要身份验证。
在
了解更多信息System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode
statusCode,字符串响应)在
System.Net.Mail.MailCommand.Send(SmtpConnection
conn,Byte []命令,字符串来自)
在
System.Net.Mail.SmtpTransport.SendMail(MailAddress
发件人,MailAddressCollection
收件人,字符串deliveryNotify ,
SmtpFailedRecipientException&
异常)在
处System.Net.Mail.SmtpClient.Send(MailMessage
消息)在
TestMailServer.Program.Main(String [] $ D:Visual Studio
2010\项目\TestMailServer\TestMailServer\Program.cs:line
中的b $ b args 26按任意键继续。 。 。
Unhandled Exception: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at TestMailServer.Program.Main(String[] args) in D:\visual studio 2010\Projects\TestMailServer\TestMailServer\Program.cs:line 26 Press any key to continue . . .
推荐答案
我不确定是什么引起了您的问题。这是我一直用于通过gmail帐户成功发送电子邮件的一些代码:
I'm not sure what is causing your problem. Here is some code I have been using to successfully send email through a gmail account:
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);
}
这篇关于为什么我使用Google SMTP无法发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!