如何将pdf文件添加为电子邮件的附件

时间:2012-07-25 05:22:33

标签: c# asp.net

我将每个人的报告动态存储为pdf,并应将其发送给attached with email。如何将其作为附件发送到电子邮件。这是我的代码。

public void Esendmail(string EmailFrom, string EmailTo, string EmailBody, string EmailSubject, string EmailCC)
{


            MailMessage message = new MailMessage();
            message.From = new MailAddress(EmailFrom);
            message.CC.Add(EmailCC);

            message.To.Add(new MailAddress(EmailTo));
            message.IsBodyHtml = true;
            message.Body = EmailBody;
            message.Subject = EmailSubject;



            SmtpClient client = new SmtpClient();


            client.Send(message);



 }

2 个答案:

答案 0 :(得分:13)

使用此示例代码

MailMessage message = new MailMessage();
message.To = "abc@domain.com";
message.From = "xyz@domain.com";
message.Subject = "mail with pdf";
message.Body = "your pdf attached";
message.Attachments.Add(new Attachment(@"c:\pdftoattach.pdf"));

SmtpMail.SmtpServer = "mail.domain.com";
SmtpMail.Send(message);

答案 1 :(得分:0)

MailMessage message = new MailMessage();
FileStream fileStream = File.Create("PdfPath");
var memoryStream = new MemoryStream();
fileStream.Position = 0;
fileStream.CopyTo(memoryStream);
message.Attachments.Add(new Attachment(memoryStream,Path.GetFileName("PdfPath")));