The error is pretty clear. You'll need to create a Attachment object. See the official docs for sample code.
How Do I Return ContentDisposition.FileName -PDF from a Method To be Attached to MailMessage
abiodunajai
396
Reputation points
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;
}
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;
}
}
Thank you for your anticipated quick response.
Best regards,
Abiodunajai
Developer technologies | ASP.NET | ASP.NET API
448 questions
Developer technologies | ASP.NET | Other
3,601 questions
Accepted answer
1 additional answer
Sort by: Most helpful
-
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