如何使用angularjs发布blob并在C#中接收blob到电子邮件

时间:2018-10-03 13:21:27

标签: c# angularjs html2canvas

我想将html2canvas捕获的图像发布到我的C#控制器,接收并将其插入电子邮件正文中,准备发送。

我正在尝试使用angularjs发布从html2canvas返回的base64转换为DataURL()函数的Blob。我相信我应该将其发布为FormData(),以便在c#中可以接收它并将其重建为图像以显示在电子邮件正文中。

this之后,它建议将base64转换为Blob,但在c#中将“ body”接收为“ null”。收件人和主题正确填充,但只有正文被接收为“空”。我试图传递base64字符串,该字符串解释了控制器中的getEmbeddedImage函数。我想尝试使用FormData(),但找不到任何信息来接收FormData()并构建要显示给用户的Blob。

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");
        });

在我的控制器中,我不确定要为重载“ body”输入哪种类型,以及如何将其传递给angularjs一侧:

  [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;
    }

我已经看过:How to read FormData C#但是,对于重建blob,我还是不清楚,我是否需要一个blob构造函数库,并通过FormData的内容设置其每个属性然后在体内显示这些数据?

2 个答案:

答案 0 :(得分:1)

我发现使用ajax可以发送base64,这就是我所做的!

        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!');
                }
            }
        });

在C#端,我使用了相同的旧代码,但是使用了以下代码,而不是mm.AlternateViews.Add(getEmbeddedImage(body));线。

mm.Body = "<img src='" + body + "' alt='image'/>";

答案 1 :(得分:0)

使用模型

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

[HttpPost]
public void EmailQuote(Model model)