How Do I Return ContentDisposition.FileName -PDF from a Method To be Attached to MailMessage

abiodunajai 371 Reputation points
2023-01-23T18:03:39.4366667+00:00

Kindly show me how to achieve this.

I have a method SendRenewalNotification() that sends Policy Renewal Notification to Clients. This Method calls another Method called PolicyScheduleGenerator() that Generates PDFs to be attached to MailMessage.

 	private void SendRenewalNotification(MessageItemWithState m, string id, string policyCode, string clientCode, string policyRefCode, string insurerCode)
        {
            var mailItem = new MailMessage();

                mailItem.From = new MailAddress(mailSetting.From, mailSetting.FromName);
                mailItem.IsBodyHtml = !String.IsNullOrEmpty(m.HtmlBody);
                mailItem.Priority = MailPriority.Normal;
                mailItem.Subject = m.Subject;
                
                //Generate the PDF to be attached to the email based on the Policy Type the client has
                var pdfattachment = PolicyScheduleGenerator(id, policyCode, clientCode, policyRefCode, insurerCode, Constants.PDFDocumentTypes.PrivateMotorPolicyScheduleDocProrata);
                mailItem.Attachments.Add(pdfattachment);

                var mailService = new SmtpClient();
                mailService.Host = mailSetting.Server;
                mailService.Port = mailSetting.Port;
                mailService.EnableSsl = mailSetting.UseSsl;

	}

My challenge is, after Generating the PDF, how do I attach it to the MailMessage?

Where the VS underline in red in the image below is my Challenge.

The PolicyScheduleGenerator() is pasted below.


        private string PolicyScheduleGenerator(string id, string policyCode, string clientCode, string policyRefCode, string insurerCode, string documentTypeName)
        {
             PolicySchedulePrintContainer detailsForPrint = _underwritingService.GetPrivateMotorPolicyDetailForPrint(id, policyCode, clientCode, policyRefCode, insurerCode, Constants.PDFDocumentTypes.PrivateMotorPolicyScheduleDocProrata);

           if (detailsForPrint.PolicySchedulePrintDTO.TotalProrataPremium.HasValue && detailsForPrint.PolicySchedulePrintDTO.TotalProrataPremium.Value > 0)
            {
                detailsForPrint = _underwritingService.GetClientPolicyScheduleDetailForPrint(id, policyCode, clientCode, policyRefCode, insurerCode, Constants.PDFDocumentTypes.PrivateMotorPolicyScheduleDocProrata);
                var result = Helper.Serialize(detailsForPrint.PolicySchedulePrintDTO);
                result = Helper.MassageXml(result, typeof(PolicySchedulePrintDTO).Name, Constants.PDFDocumentTypes.PrivateMotorPolicyScheduleDocProrata);
                var resultByteArray = UTF8Encoding.UTF8.GetBytes(result);
                var xmapByteArray = UTF8Encoding.UTF8.GetBytes(detailsForPrint.PDFDocumentXMAP.DocumentXMAP);
                var documentByte = new PDFEngine().GeneratePDF(xmapByteArray, resultByteArray, Constants.PDFDocumentTypes.PrivateMotorPolicyScheduleDocProrata);

                var memStream = new MemoryStream(documentByte);
                memStream.Seek(0, SeekOrigin.Begin);
                var contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
                var reportAttachment = new Attachment(memStream, contentType);
                reportAttachment.ContentDisposition.FileName = "PrivateMotorPolicySchedule.pdf";
                return reportAttachment.ContentDisposition.FileName;
            }
         }

@LiHongMSFT-4306 @Lihong Meng @Lihong Meng @LiHong Russo @m@MFST Support

Thank you all in anticipation of your quick response.

Best regards,

Abiodunajai

RenewalNotification

policyScheduleGenerator2

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
318 questions
0 comments No comments
{count} votes