gpt4 book ai didi

c# - 如何使用 Gmail SMTP 服务器在 C# 中发送邮件?

转载 作者:太空狗 更新时间:2023-10-29 23:11:03 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Sending email through Gmail SMTP server with C#

对于使用 C# 发送邮件和使用 Gmail SMTP 服务器,我们应该做一些棘手的事情吗?因为经过大量搜索,我找到了一些方法来执行此操作,但结果是我遇到了失败异常。我想这是因为我没有为 Gmail 处理 TSL(因为它与 TSL 一起工作),但我不知道如何用 C# 处理 TSL 来做到这一点。我非常感谢任何帮助或指向有用示例的链接。这是我的代码:

public string SendMail(string senderMail, string receiverMail, string attachmentPath)
{
var fromMailAddress = new MailAddress(senderMail);
var toMailAddress = new MailAddress(receiverMail);

MailMessage mailMessage = new MailMessage(fromMailAddress, toMailAddress);
mailMessage.Subject = "My Subject";
mailMessage.Body = "This is the body of this message for testing purposes";

Attachment attachFile = new Attachment(attachmentPath);
mailMessage.Attachments.Add(attachFile);

SmtpClient emailClient = new SmtpClient();

NetworkCredential credential = new NetworkCredential();
credential.UserName = fromMailAddress.User;
credential.Password = "password";

emailClient.Credentials = credential;
emailClient.Port = 587;
emailClient.Host = "smtp.gmail.com";

//emailClient.EnableSsl = true; //Here should be for TSL, but how?

emailClient.Send(mailMessage);
}

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