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

abiodunajai 396 Reputation points
2023-01-23T18:37:21.48+00:00

Please I have a method SendRenewalNotification() that calls another Method PolicyScheduleGenerator() to get PDF that it will attach to a MailMessage.

The SendRenewalNotification() is pasted below:

 	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;

	}

RenewalNotification

The PolicyScheduleGenerator() is pasted below.

My challenge is :

How do I attach the return PDF to the MailMessage?

The item underlined in red in the image above is my challenge.

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

policyScheduleGenerator2

Thank you for your anticipated quick response.

Best regards,

Abiodunajai

Developer technologies | ASP.NET | ASP.NET API
Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-01-23T20:07:47.23+00:00

    The error is pretty clear. You'll need to create a Attachment object. See the official docs for sample code.

    https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.attachments?view=net-7.0#examples


1 additional answer

Sort by: Most helpful
  1. abiodunajai 396 Reputation points
    2023-01-23T20:03:40.31+00:00

    Thank you AgaveJoe. The 2 error messages are:

    Argument 1: cannot convert from 'string' to 'System.Net.Mail.Attachment'
    
         2) The best overloaded method match for 'System.Collections.ObjectModel.Collection<System.Net.Mail.Attachment>.Add(System.Net.Mail.Attachment)' has some invalid arguments
    
    Best Regards
    
    Abiodunajai
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.