我正在使用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);
}
5条答案
按热度按时间dly7yett1#
通过
new MailMessage
方法调用创建的message
对象具有属性.Attachments
。例如:
vshtjzan2#
使用MSDN中建议的Attachment类:
e4yzc0pl3#
像这样更正代码
http://csharp.net-informations.com/communications/csharp-email-attachment.htm
希望这对你有帮助。
里基
htzpubme4#
提示:如果在之后添加附件,则附件文件路径会覆盖邮件正文,因此请先添加附件,后添加正文
邮件.附件.添加(新建附件(文件));
邮件。正文=“body”;
mnowg1ta5#
一行回答: