Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add pdf File as an Attachment to an Email

Tags:

c#

asp.net

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

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

 }
like image 260
nitha raj Avatar asked Jul 25 '12 05:07

nitha raj


2 Answers

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);
like image 195
Cdeez Avatar answered Nov 15 '22 03:11

Cdeez


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")));
like image 4
Lovindu Bandara Avatar answered Nov 15 '22 02:11

Lovindu Bandara