1

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?

Anonymous
  • 61
  • 3

2 Answers2

1

I found that using ajax you can send a base64, so that's what I did!

        img = canvas.toDataURL();

        var data = {

            recipientEmail: "sample@sample.com",

            subject: "test mail",

            body: img

        };
        $.ajax({
            url: '/Home/EmailQuote',
            type: "POST",
            dataType: 'json',
            data: data,
            success: function (response) {
                if (response.success) {
                    alert('Email sent!');
                }
            }
        });

In the C# side I used the same old code but used the below code instead of the mm.AlternateViews.Add(getEmbeddedImage(body)); line.

mm.Body = "<img src='" + body + "' alt='image'/>";
Anonymous
  • 61
  • 3
0

Use a model

public class Model
{
    public string RecipientEmail { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

[HttpPost]
public void EmailQuote(Model model)
Alexandr Sulimov
  • 1,894
  • 2
  • 24
  • 48