SSIS - Freshdesk 的 REST API [英] SSIS - REST API for Freshdesk

查看:26
本文介绍了SSIS - Freshdesk 的 REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在这里搜索一些东西,但似乎没有什么适合我的需要.我创建了一个运行报告的 SSIS 包 ->将其附加到电子邮件中并多次发送给一群人(不同的收件人、不同的文件).

I tried searching something here but nothing seems to fit my need. I created an SSIS package that runs a report -> Attaches it to an email and send it to a bunch of people, many times (different recipients, different files).

由于 Zapier 的限制,我无法创建带有附件的 FreshDesk 票证,这对我来说是必须的,所以我正在探索 FreshDesk API,但我不是 c# 开发人员.我在网上找到了一些例子,现在我试图适应这个代码:FreshSamples C-Sharp 创建带有附件的票证到我现有的代码中,希望将我所有的变量作为工单字段传递 '

Due to Zapier limitations I can't create a FreshDesk ticket with attachments and that is for me a must to have so I'm exploring FreshDesk API, but I'm no c# developer. I found some examples online and now I'm trying to fit this code: FreshSamples C-Sharp Create Ticket with attachment into my existing code, hoping to pass all my variable as ticket fields '

    #region VSTA generated code
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
    #endregion


    public void Main()
    {

        // initialize StreamReader class for text file
        StreamReader streamReader = new StreamReader(Dts.Variables["User::MHTMailPath"].Value.ToString());
        // Read the StreamReader To End and assign to local variable
        string StreamText = streamReader.ReadToEnd();
        // assign SSIS variable with value of StreamText local variable.
        this.Dts.Variables["User::HTMLMail"].Value = StreamText;

        // TODO: Add your code here
        MailMessage email = new MailMessage();



        if ((Dts.Variables["User::MHTEmail1"].Value.ToString() != null) && (Dts.Variables["User::MHTEmail1"].Value.ToString() != string.Empty))
        {
            email.To.Add(Dts.Variables["User::MHTEmail1"].Value.ToString());
        }
        if ((Dts.Variables["User::MHTEmail2"].Value.ToString() != null) && (Dts.Variables["User::MHTEmail2"].Value.ToString() != string.Empty))
        {
            email.To.Add(Dts.Variables["User::MHTEmail2"].Value.ToString());
        }
        if ((Dts.Variables["User::MHTEmail3"].Value.ToString() != null) && (Dts.Variables["User::MHTEmail3"].Value.ToString() != string.Empty))
        {
            email.To.Add(Dts.Variables["User::MHTEmail3"].Value.ToString());
        }
        if ((Dts.Variables["User::MHTEmail4"].Value.ToString() != null) && (Dts.Variables["User::MHTEmail4"].Value.ToString() != string.Empty))
        {
            email.To.Add(Dts.Variables["User::MHTEmail4"].Value.ToString());
        }

        //email.CC.Add(CCAddresses);
        email.From = new MailAddress("xxx@xxx.com");
        email.Subject = Dts.Variables["User::MHTCd"].Value.ToString() + " - " + Dts.Variables["User::MHTCustomerDS"].Value.ToString() + " R" + Dts.Variables["User::Period"].Value.ToString().Trim().Substring(Dts.Variables["User::Period"].Value.ToString().Trim().Length - 2, 2);
        email.IsBodyHtml = true;
        email.Body = Dts.Variables["User::HTMLMail"].Value.ToString(); 
        string reportFile = Dts.Variables["User::ReportFile"].Value.ToString();
        try
        {
            //For MHT file. Decode MHTML to HTML and embed in email body
            if (Path.GetExtension(reportFile) == ".mht")
            {
                var decodedHtml = new StringBuilder();
                using (var reader = new StreamReader(reportFile))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        if (line != "Content-Transfer-Encoding: base64") continue;

                        reader.ReadLine();
                        while ((line = reader.ReadLine()) != String.Empty)
                            if (line != null)
                                decodedHtml.Append(
                                    Encoding.UTF8.GetString(
                                        Convert.FromBase64String(line)));
                        break;
                    }
                }
                email.Body = email.Body + Environment.NewLine + decodedHtml.ToString();
                email.IsBodyHtml = true;
            }

            else
            {
                //Attach the file
                Attachment attachmentFile = new Attachment(reportFile);
                email.Attachments.Add(attachmentFile);
            }
        }
        catch (Exception e)
        {
            Dts.Events.FireError(0, "create email message", e.Message, String.Empty, 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }

        //System.Diagnostics.Process proc = new System.Diagnostics.Process();
        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
        smtpClient.EnableSsl = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("xxx@xxx.com", "xxxxxxx");
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        try
        {
            smtpClient.Send(email);
            //email.Attachments.Dispose();
            //File.Delete(reportFile);
            Dts.TaskResult = (int)ScriptResults.Success;
        }
        catch (Exception e)
        {
            Dts.Events.FireError(0, "smtp emailing", e.Message, String.Empty, 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
        finally
        {
            if (email != null)
            {
                email.Dispose();
            }
            if (smtpClient != null)
            {
                //smtpClient.Dispose();
            }
        }
        Dts.TaskResult = (int)ScriptResults.Success;
    }

}

但是我在试图让我的 reportFile 被正确获取"时失去了理智;通过 FreshDesk 示例,它需要文件系统上的文件.此外,我需要附加更多的文件,所以我想知道是否有好心人会为我指明正确的方向.

but I'm losing my mind trying to get my reportFile to be correctly "taken" by FreshDesk sample which expects a file on the filesystem. Moreover, I will need to attach more that one file so I was wondering if any good samaritan would point me in the right direction.

提前致谢

推荐答案

多次调用 email.Attachments.Add() 添加多个附件就可以了:

It will be okay to call email.Attachments.Add() multiple times to add multiple attachments:

关键部分是:

要将现有文件转换为附件:

To turn an existing file into an attachment:

    Attachment attachmentFile = new Attachment(reportFile);
    email.Attachments.Add(attachmentFile);

要将字符串块转换为附件:

To turn a string block into an attachment:

using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
    writer.Write(reportFile);
    writer.Flush();
    stream.Position = 0;

    email.Attachments.Add(new Attachment(stream, "myreport-xyz.txt", "text/plain"));
}

这篇关于SSIS - Freshdesk 的 REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆