添加附件使用C#电子邮件 [英] Adding an attachment to email using C#

查看:276
本文介绍了添加附件使用C#电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的这个答案在发送电子邮件以下code。 NET通过Gmail 的。我有麻烦的是添加附件的电子邮件。使用下面的code我如何添加附件?

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);
}

在此先感谢。

Thanks in advance.

推荐答案

的消息新MailMessage创建的对象方法调用有一个属性 .Attachements

The message object created from your new MailMessage method call has a property .Attachements.

例如:

message.Attachments.Add(new Attachment(PathToAttachment));

这篇关于添加附件使用C#电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆