使用 C# 向电子邮件添加附件 [英] Adding an attachment to email using C#
本文介绍了使用 C# 向电子邮件添加附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此答案中的以下代码 通过 Gmail 在 .NET 中发送电子邮件.我遇到的问题是在电子邮件中添加附件.我将如何使用下面的代码添加附件?
I'm using the following code from this answer Sending email in .NET through Gmail. The trouble I'm having is adding an attachment to the email. How would I add an attachment using the code below?
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代码>.
The message
object created from your new MailMessage
method call has a property .Attachments
.
例如:
message.Attachments.Add(new Attachment(PathToAttachment));
这篇关于使用 C# 向电子邮件添加附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文