首页 > 解决方案 > 在 SSIS 包中将 SendGrid 与脚本任务一起使用

问题描述

通过使用 VS 2017,我创建了一个 SSIS 包,然后尝试将 SendGrid 与 Script Task 组件一起使用,但我一直有错误消息。错误消息是“调用的目标已引发异常”。以下是我的代码,请帮我看看它有什么问题。谢谢你。

public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
    static EmailAddress yao = new EmailAddress("user@sample.com", "User Sample");
    static string template_id = "SendGridTemplateID";
    static string _api_key = ConfigurationManager.AppSettings["SendGridKey"];
    public void Main()
    {

        SendEmail().Wait();
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    static async Task SendEmail()
    {

        var client = new SendGridClient(_api_key);
        var msg = new SendGridMessage();
        msg.AddTo(new EmailAddress("Sample@user.com", "Sample User"));
        msg.SetFrom(new EmailAddress("Sample@user.com", "Sample User"));

        msg.SetTemplateData(GoogleLinkReport);
        msg.SetTemplateId(template_id);

        var response = await client.SendEmailAsync(msg);
        Console.WriteLine(response.Body.ReadAsStringAsync().Result);

    }
    static GoogleLink GoogleLinkReport = new GoogleLink
    {
        header = "This is google link",
        text = "Please click the button to open the link for google",
        subject = "GoogleLink",
        c2a_link = "https://www.google.com",
        c2a_button = "Google Link"
    };
    public class GoogleLink
    {
        public string header;
        public string text;
        public string subject;
        public string c2a_link;
        public string c2a_button;
    }

}

标签: c#visual-studiossissendgrid

解决方案


推荐阅读