为什么我使用Google'smtp'无法发送电子邮件? [英] Why I use google 'smtp' cannot send out email?
问题描述
我有以下程序通过使用"smtp.gmail.com:587"发送电子邮件.
I have the following program to send an 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(SmtpStatusCodestatusCode,字符串响应)位于System.Net.Mail.MailCommand.Send(SmtpConnectionconn,Byte []命令,来自的字符串)
在System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人,MailAddressCollection收件人,字符串传送通知,SmtpFailedRecipientException&例外)System.Net.Mail.SmtpClient.Send(MailMessage讯息)TestMailServer.Program.Main(String []args)在D:\ visual studio中2010 \ Projects \ TestMailServer \ TestMailServer \ Program.cs:line26按任意键继续..
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屋!