Reputation: 8255
Below I've added a screenshot of the attachment as it appears in the email.
It opens fine in any PDF reader.
How do I get it to present as an actual PDF? I feel as though I've missed something small...
Here's my code:
public ActionResult SendInvoice(SendInvoiceViewModel model)
{
var Invoice = db.Invoices.FirstOrDefault(x => x.Id == model.Id);
MemoryStream ms = new MemoryStream(Invoice.Document);
Attachment Data = new Attachment(ms, MediaTypeNames.Application.Pdf);
ContentDisposition Disposition = Data.ContentDisposition;
Disposition.CreationDate = Invoice.Date;
Disposition.ModificationDate = Invoice.Date;
Disposition.ReadDate = Invoice.Date;
SendInvoiceMail(model.EmailAddress, Invoice.Number, model.EmailMessage, Data);
}
private void SendInvoiceMail(string emailAddress, string invoiceNumber, string message, Attachment attachment)
{
using (MailMessage Message = new MailMessage())
{
Message.From = new MailAddress("accounts@###########");
Message.Subject = String.Format("Your store invoice {0}", invoiceNumber);
Message.To.Add(new MailAddress(emailAddress));
Message.Body = message;
Message.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient("mail.############", 587);
smtp.Credentials = new NetworkCredential("info@##########", "##########");
smtp.Send(Message);
};
}
So what did I miss?
Upvotes: 0
Views: 109
Reputation: 247531
Try
public ActionResult SendInvoice(SendInvoiceViewModel model)
{
var Invoice = db.Invoices.FirstOrDefault(x => x.Id == model.Id);
MemoryStream ms = new MemoryStream(Invoice.Document);
//Construct a file name for the attachment
var filename = string.Format("{0}.pdf", Invoice.Number);
Attachment Data = new Attachment(ms, filename, MediaTypeNames.Application.Pdf);
ContentDisposition Disposition = Data.ContentDisposition;
Disposition.CreationDate = Invoice.Date;
Disposition.ModificationDate = Invoice.Date;
Disposition.ReadDate = Invoice.Date;
SendInvoiceMail(model.EmailAddress, Invoice.Number, model.EmailMessage, Data);
}
Where you include the file name for the attachment.
Upvotes: 0
Reputation: 108841
Try using the 3-parameter version of the Attachment()
constructor. The second parameter lets you give a file name. That file name should end in .pdf
.
Upvotes: 1