PDF file in email content
I have put the invoice as a PDF file in the email content, but when I open the PDF, the file does not open even when I download it. Is there something wrong with the code?
public bool Email_Sending(EmailBase email, dynamic? args) {
// E-mail naar klant;
if (CurrentPageID() == "edit") {
email.Sender = "dp@selexions.nl";
email.Recipient = "aiden.messi23@gmail.com";
email.Cc = "";
email.Bcc = "";
email.Subject = "factuur test";
email.Content = "Hierbij ontvangt u de factuur in de onderstaande bijlage";
email.Format = "html";
email.Charset = "utf-8";
// HTML content voor de PDF
string htmlContent = @"
<!DOCTYPE html>
<html lang='nl'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Factuur</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background: #f4f4f4;
}
img#logo {
display: none !important;
}
.main-footer {
display: none !important;
}
.container5 {
margin: 0 auto;
padding: 20px;
}
.header5 {
text-align: center;
margin-bottom: 20px;
}
.header-image5 {
width: 100%;
display: block;
margin: 0 auto;
}
.contact-info5 {
background: #fd7e14;
padding: 10px;
text-align: center;
margin-bottom: 20px;
}
.contact-info5 p {
margin: 0;
white-space: nowrap;
}
.details5 {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.details5 .left5, .details5 .right5 {
width: 48%;
}
.details5 .right5 {
text-align: right;
}
.invoice5 {
margin-bottom: 20px;
}
.invoice-header5, .invoice-item5 {
display: flex;
justify-content: space-between;
padding: 8px 0;
word-break: break-word;
}
.invoice-header5 {
font-weight: bold;
border-bottom: 1px solid #000;
}
.invoice-item5 span {
white-space: pre-wrap;
margin-top: -40px;
}
.totals5 {
text-align: right;
margin-top: 125px;
}
.totals5 .total-item5 {
display: flex;
justify-content: space-between;
}
.bank-info5 {
margin-bottom: 20px;
float: left;
text-align: left;
margin-top: 125px;
}
.bank-info5 p {
margin: 5px 0;
}
.payment-info {
margin-top: 20px;
font-weight: bold;
}
.right, .right-extended {
display: flex;
justify-content: flex-end;
align-items: center;
}
.right > div, .right-extended > div {
text-align: right;
white-space: nowrap;
width: 108px;
}
.right-extended > div > p:first-child {
border-bottom: 1px solid #000;
padding-bottom: 4px;
}
</style>
</head>
<body>
<div class='container5'>
<div class='header5'>
<a href='https://www.klankschalenlimburg.nl/' target='_blank'><img src='https://www.selexions.nl/uploadfiles/klank.jpg' alt='Klankschalen Limburg' style='width: 100%; display: block; margin: 0 auto;' /></a>
<div class='contact-info5'>
<p>Barbarastraat 26, Geleen, Telefoonnummer: 046-4110136, E-mail: info@klankschalenlimburg.nl</p>
</div>
</div>
<div class='details5'>
<div class='left5'>
<p>{{{dbvalue FactuurNaam}}}</p>
<p>{{{dbvalue FactuurAdres}}}</p>
<p>{{{dbvalue FactuurPostcode}}}</p>
<p>{{{dbvalue FactuurPlaats}}}</p>
</div>
<div class='right5'>
<p>Geleen, {{{value FactuurDatum}}}</p>
</div>
</div>
<div class='invoice5'>
<div class='invoice-header5'>
<span>Beschrijving</span>
</div>
<div class='invoice-item5'>
<span>{{{value FactuurOmschrijving}}}</span>
</div>
</div>
<div class='totals5'>
<div class='bank-info5'>
<p>Rabobank Geleen NL88RABO01160.10.568</p>
<p>KvK Zuid-Limburg 14052069</p>
<p>BTW-nummer 8140.10.532.B01</p><br><br><br>
</div>
<div class='right-extended'>
<div>
<p>Sub-totaal</p>
<p>{{{dbvalue FactuurPrijs}}}</p>
</div>
<div>
<p>BTW 21%</p>
<p>{{{dbvalue FactuurBtwBedrag}}}</p>
</div>
<div>
<p>Totaal</p>
<p>{{{dbvalue FactuurPrijsInclusiefBtw}}}</p>
</div>
</div>
</div>
<div class='payment-info'>
<p>Gelieve te betalen voor {{{dbvalue FactuurDatumBetalingstermijn}}}, ondervermelding van uw naam en factuurdatum.</p>
</div>
</div>
</body>
</html>";
// Generate PDF
string pdfFileName = "Factuur.pdf";
using (MemoryStream memoryStream = new MemoryStream()) {
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
// Convert the MemoryStream to a byte-array
byte[] pdfBytes = memoryStream.ToArray();
// Attach PDF to email
string pdfContent = Convert.ToBase64String(pdfBytes);
email.AddAttachment(pdfFileName, pdfContent);
}
}
return true;
}