C# 如何将 pdf 文件作为附件添加到电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11643231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:37:54  来源:igfitidea点击:

How to Add pdf File as an Attachment to an Email

c#asp.net

提问by nitha raj

I am dynamically storing each person report as pdfand should send it to them an email Attacment. How do I send it as an attachment to an email. Here is my code.

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

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

 }

回答by Cdeez

Use this sample code

使用此示例代码

MailMessage message = new MailMessage();
message.To = "[email protected]";
message.From = "[email protected]";
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);

回答by Lovindu Bandara

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