I want to post my image captured by html2canvas to my c# controller, receive it and insert it in the email body, ready to send.
I am trying to use angularjs to post a blob that has been converted from the base64 returned by html2canvas toDataURL() function. I believe I should possibly post it as a FormData() so that in c# I can receive it and reconstruct it as an image to display in the email body.
Following this it suggested to convert the base64 to a blob but "body" is received as "null" in c#. The recipient and the subject is populated correctly but only the body is received as "null". I tried to pass a base64 string which explains the getEmbeddedImage function in my controller. I want to try using FormData() but I could not find any information to receive the FormData() and build the blob to be displayed to the user.
Angularjs:
html2canvas($('#quoteTable')[0], {
letterRendering: 1,
allowTaint: true,
width: 1600,
height: 1800
}).then(function (canvas) {
img = canvas.toDataURL();
var tempImg = img;
var base64ImageContent = tempImg.replace(/^data:image\/(png|jpg);base64,/, "");
var blob = $scope.base64ToBlob(base64ImageContent, 'image/png');
//var formData = new FormData();
//formData.append('picture', blob);
var data = {
recipientEmail: "sample@sample.co.uk",
subject: "test mail",
body: blob
};
$http.post('/Home/EmailQuote', JSON.stringify(data)).then(function (response) {
if (response.data)
$scope.msg = "Post Data Submitted Successfully!";
}, function (response) {
$scope.msg = "Service not Exists";
$scope.statusval = response.status;
$scope.statustext = response.statusText;
$scope.headers = response.headers();
});
var win = window.open();
win.document.open();
win.document.close();
})
.catch(function (error) {
/* This is fired when the promise executes without the DOM */
alert("could not generate canvas");
});
In my controller I am not sure what type to put for the overload "body" and how I will pass it in on the angularjs side:
[HttpPost]
public void EmailQuote(string recipientEmail, string subject, string body)
{
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("sample@gmail.com", "password");
MailMessage mm = new MailMessage();
mm.From = new MailAddress("sample@sample.co.uk");
mm.To.Add(recipientEmail);
mm.Subject = subject;
mm.IsBodyHtml = true;
mm.AlternateViews.Add(getEmbeddedImage(body));
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
try
{
client.Send(mm);
ViewBag.MyProperty = "Successfully sent email";
}
catch (SmtpException ex)
{
ViewBag.Message = "Exception caught: " + ex;
}
}
private AlternateView getEmbeddedImage(String filePath)
{
LinkedResource res = new LinkedResource(filePath);
res.ContentId = Guid.NewGuid().ToString();
string htmlBody = @"<img src='cid:" + res.ContentId + @"'/>";
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(res);
return alternateView;
}
I have looked at this: How to read FormData C# However, it becomes unclear to me when it comes to rebuilding the blob, would I need a library for a blob Constructor and set each attribute of it by the contents of the FormData then display that data in the body?