我正在使用此答案 Sending email in .NET through Gmail 中的以下代码.我遇到的麻烦是在电子邮件中添加附件。如何使用下面的代码添加附件?
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "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);
}
提前致谢。
最佳答案
从您的 new MailMessage
方法调用创建的 message
对象具有属性 .Attachments
。
例如:
message.Attachments.Add(new Attachment(PathToAttachment));
关于c# - 使用 C# 向电子邮件添加附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5034503/