Reputation: 8636
I have a code where it will generate an HTML content and saves it in an location as follows
I also tried by manually copying the html text and tried but no luck, I am getting bad request, So can some one help me how to send the framed html content as an attachment using SendGrid. The same piece of code works when I use smtp client with out SendGrid
Here is the updated code, I see a property checked=""
when I replace this manually it works but through code I am trying it is not
public static void SendMail()
{
try
{
var apiKey = ""; //insert your Sendgrid API Key
var client = new SendGridClient(apiKey);
var from = new EmailAddress("[email protected]");
var tosList = new List<EmailAddress>();
var ToEMailAddress = "[email protected]";
if (ToEMailAddress.Contains(";"))
{
foreach (var address in ToEMailAddress.Split(';'))
{
tosList.Add(new EmailAddress(address));
}
}
else
{
tosList.Add(new EmailAddress(ToEMailAddress));
}
var plainTextContent = string.Empty;
var htmlContent = "Test Body";
var subject = "Test Subje";
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, tosList, subject, plainTextContent, htmlContent);
var fileName = "BmaReport_" + "_" + DateTime.Now.ToString("MMMddyyyyHHmmss") + ".html";
string content = @"<!DOCTYPE html><html><head><style type='text/css'>table { font-family: Noto Sans;border-collapse: collapse;width: 100 %;} table td, table th { border: 1px solid #ddd;padding: 8px;}table tr:nth-child(even){background - color: #f2f2f2;} table tr:hover { background - color: #ddd;} table th { padding - top: 12px;padding - bottom: 12px;text - align: left;background - color: #04AA6D;color: white;}.togCheck { display: none; }.togContent { max-height: 0; opacity: 0;visibility: hidden;transition: max-height 1s;}.togCheck:checked + .togContent {max-height: 162vh; opacity: 1; visibility: visible;}{font-family: Noto Sans;box-sizing: border-box;}.togButton { display: block; width: 100 %;padding: 10px;color: #fff;background: #00778D;cursor: pointer;}.togContent { background: #fefefe00;}.togContent p { padding: 50px 10px;margin: 0;}</style></head><body style='font-family: Noto Sans'><br><br> Record of report submitted via BMA Reporting Portal at 01:54UTC on 19Aug2022<div style='height: 85px;width: 194px;float: right;margin-right: 2%;margin-top: -1%;background-image: url(https://stage-casualtyreporting.azurewebsites.net/Assets/logo-bma.svg);background-repeat: no-repeat;'></div><br><br> Record submitted to BMA<table><tr><td>Reportable Event : Loss of hull integrity</td></tr></table><br><br><h5 for='vesselDetail' class='togButton'>Principal Details</h5><input type='checkbox' checked="" class='togCheck' id='vesselDetail'><div class='togContent'><h5>Vessel and Voyage details</h5><table cellpadding='5' cellspacing='5'><tr><td> Vessel Name : Test loss</td><td> IMO No : 3333333</td><td> Vessel Type : Bulk carrier</td><td> Flag : Bahamas</td></tr><tr><td> No. of Crew on Board : 1</td><td> No. of Passengers : 0</td><td> No. of Supernumeraries : 0</td><td> Voyage From : Test loss</td></tr><tr><td> Voyage to/ Current port (if alongside) : Test loss</td><td> Cargo (if applicable) : </td></tr></table><h5>Reportable Event Details</h5><table cellpadding='5' cellspacing='5'><tr><td> Stage of Passage : Anchored</td><td>Date of Event : 08-01-2022</td></tr><tr><td> Latitude Degree : 33</td><td> Latitude Decimal : 33</td><td> Latitude Direction : N</td></tr><tr><td> Longitude Degree : 33</td><td> Longitude Decimal : 33</td><td> Longitude Direction : E</td><td>What was happening at the time of event (select all that apply) : Ballasting,Bunkering,</td></tr><tr><td>Time of Event (ship/local) : 03:33</td><td>Time Zone : +6</td></tr><tr><table><tr><td>Brief Description : Test loss</td></tr></table></tr></table><h5>Additional consequences of event</h5><table cellpadding='5' cellspacing='5'><tr><td>Injured Person(s) :No</td><td>Pollution :No</td><td>Missing Person : No</td><td>Fatality : No</td><td>Material Damage : No</td></tr></table></div><h5 for='envDetail' class='togButton'>Environment Details</h5><input type='checkbox' checked="" class='togCheck' id='envDetail'><div class='togContent'><table cellpadding='5' cellspacing='5'><tr><td> Weather : Overcast</td><td> Visibility : Poor (Vis 0.5−2nm)</td><td> Light : Daylight</td></tr><tr><td> Wind Speed (Beaufort scale) : 1 (1−3 knots)</td><td> Wind Direction : N</td><td> Sea State : Rippled (0–0.1m)</td></tr></table></div><h5 for='contDet' class='togButton'>Contact Details</h5><input type='checkbox' checked="" class='togCheck' id='contDet'><div class='togContent'><h5>DPA Contact Detail</h5><table cellpadding='5' cellspacing='5'><tr><td>Name : Test loss</td><td>DPA Email : [email protected]</td><td>DPA Phone No : 1234567890</td></tr></table><table><h5>Master's Contact Details</h5></table><table cellpadding='5' cellspacing='5'><tr><td>Name : Test loss</td><td>DPA Email : [email protected]</td><td>DPA Phone No : 1234567890</td></tr></table><h5>Person Completing Form</h5><table cellpadding='5' cellspacing='5'><tr><td>Name : Test loss</td><td>Role : DPA</td><td>Date : 08-02-2022</td><td>Email : [email protected]</td></tr></table></div></body></html>";
content = content.Replace(@"checked=""", "checked=''");
SendGrid.Helpers.Mail.Attachment attachment = new SendGrid.Helpers.Mail.Attachment
{
Filename = fileName,
Type = "text/html",
//ContentId = "Banner",
Content = content,
Disposition = "attachment"
};
msg.Attachments = new List<SendGrid.Helpers.Mail.Attachment>
{
attachment
};
var result = Task.Run(() => client.SendEmailAsync(msg)).GetAwaiter().GetResult();
if (result != null)
{
}
}
catch (Exception ex)
{
if (ex.Message != null)
{
}
}
}
Also I am not receiving the output as expected when it was processed successfully
Upvotes: 0
Views: 784
Reputation: 8636
Seems like we need convert the string to base64 as per docs and send it. I converted it to base64 and it is working now as expected
https://docs.sendgrid.com/api-reference/mail-send/mail-send
Upvotes: 1