我正在使用此答案中的以下代码通过 Gmail 在 .NET 中发送电子邮件 https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail。我遇到的问题是在电子邮件中添加附件。如何使用下面的代码添加附件?
using System.Net.Mail;
var fromAddress = new MailAddress("[email protected] /cdn-cgi/l/email-protection", "From Name");
var toAddress = new MailAddress("[email protected] /cdn-cgi/l/email-protection", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
The message
从您创建的对象new MailMessage
方法调用有一个属性.Attachments
.
例如:
message.Attachments.Add(new Attachment(PathToAttachment));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)