使用C向电子邮件添加附件#

我使用下面的代码通过Gmail在.NET中发送电子邮件。我遇到的麻烦是在电子邮件中添加附件。如何使用下面的代码添加附件

使用System.Net.Mail;
var fromAddress=新邮件地址(“[email protected]“,”源于名称“);
var toAddress=新邮件地址(“[email protected]“,”以名称“);
常量字符串fromPassword=“fromPassword”;
常量字符串subject=“subject”;
常量字符串body=“body”;
var smtp=新SmtpClient
{
Host=“smtp.gmail.com”,
端口=587,
EnableSsl=true,
DeliveryMethod=SmtpDeliveryMethod.Network,
UseDefaultCredentials=false,
凭据=新网络凭据(fromAddress.Address,fromPassword)
};
使用(var message=新邮件消息(fromAddress,toAddress)
{
主语,
身体
})
{
smtp.Send(message);
}

提前谢谢

新MailMessage方法调用创建的消息对象具有属性.Attachments

例如:

message.Attachments.Add(新附件(PathToAttachment));

发表评论