gpt4 book ai didi

c# - 为什么我用google 'smtp'发不了邮件?

转载 作者:太空狗 更新时间:2023-10-30 00:35:04 26 4
gpt4 key购买 nike

我有以下程序使用“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com