2016-06-17 12 views
0

Ниже я добавил скриншот вложения, как он появляется в письме.Как я могу прикреплять PDF-приложение для прикрепления PDF в PDF-формате?

Он отлично открывается в любом PDF-ридере.

Как я могу представить его как фактический PDF? Я чувствую, что я пропустил что-то маленькое ...

Вот мой код:

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("[email protected]###########"); 
     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("[email protected]##########", "##########"); 
     smtp.Send(Message); 
    }; 
} 

Так что я пропустил?

Email Addtachment

+1

Я, вероятно, не вижу права, но где вы задаете свое имя? – Mekap

+0

Я не ... Не знаю, где это сделать, но за ответ Олли Джонса ниже, я готов поспорить, что я делаю это при правильной перегрузке 'Attachment()' – Ortund

ответ

1

Попробуйте использовать версию 3-параметризацию Attachment() конструктора. Второй параметр позволяет указать имя файла. Это имя файла должно заканчиваться на .pdf.

0

Попробуйте

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

Где включить имя файла для вложения.

 Смежные вопросы

  • Нет связанных вопросов^_^