c#中的示例使用sendingblue v3:sib_api_v3_sdk发送事务



我很想知道是否有人使用sib_api_v3_sdk在c#中发送事务性电子邮件,我尝试了这个代码,但我不确定:

var EmailSender = "services@magental.com";
                    var EmailTo = receiver_email;
                    var EmailToName = receiver_fullname;
                    var EmailToObject = mailinglistobject;
                    var EmailToBody = mailinglistbody;
                    var EmailToListIdSendingBlue = mailinglistidsendingblue;
                    Configuration.Default.ApiKey.Add("api-key", "MYKEY");
                    var apiInstance = new TransactionalEmailsApi();
                    string SenderName = "MagentaL Team";
                    string SenderEmail = EmailSender;
                    SendSmtpEmailSender Email = new SendSmtpEmailSender(SenderName, SenderEmail);
                    string ToEmail = EmailTo;
                    string ToName = EmailToName;
                    SendSmtpEmailTo smtpEmailTo = new SendSmtpEmailTo(ToEmail, ToName);
                    List<SendSmtpEmailTo> To = new List<SendSmtpEmailTo>();
                    To.Add(smtpEmailTo);

                    //var contactEmails = new AddContactToList(); // AddContactToList | Emails addresses OR IDs of the contacts
                    //PostContactInfo result = apiInstance.AddContactToList(EmailToListIdSendingBlue, contactEmails);

            
                    JObject attributes = new JObject();
                    attributes.Add("EMAIL", EmailTo);
                    attributes.Add("USERNAME", EmailToName);        
                    List<long?> listIds = new List<long?>();
                    listIds.Add(EmailToListIdSendingBlue);               
                        var createContact = new CreateContact(EmailToName, attributes,  listIds);
                        CreateUpdateContactModel result = apiInstance.CreateContact(createContact);
                  

                    //HtmlContent, TextContent, Subject,
                    var sendSmtpEmail = new SendSmtpEmail(Email); // SendSmtpEmail | Values to send a transactional email
                    sendSmtpEmail.To = To;
                    sendSmtpEmail.HtmlContent = EmailToBody;
                    sendSmtpEmail.Subject = EmailToObject;
                    // Send a transactional email
                    CreateSmtpEmail result = apiInstance.SendTransacEmail(sendSmtpEmail);

我不知道如何将联系人添加到我的发送蓝色列表中(例如:list id 15(

有人可以帮我用c#编写一个工作示例代码吗?非常感谢

从他们的网站复制和粘贴https://developers.brevo.com/reference/sendtransacemail

using sib_api_v3_sdk.Api;
using sib_api_v3_sdk.Client;
using sib_api_v3_sdk.Model;
using System;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace Sendinblue
{
    class Program
    {
        static void Main(string[] args)
        {
            Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");
            var apiInstance = new TransactionalEmailsApi();
            string SenderName = "John Doe";
            string SenderEmail = "example@example.com";
            SendSmtpEmailSender Email = new SendSmtpEmailSender(SenderName, SenderEmail);
            string ToEmail = "example1@example1.com";
            string ToName = "John Doe";
            SendSmtpEmailTo smtpEmailTo = new SendSmtpEmailTo(ToEmail, ToName);
            List<SendSmtpEmailTo> To = new List<SendSmtpEmailTo>();
            To.Add(smtpEmailTo);
            string BccName = "Janice Doe";
            string BccEmail = "example2@example2.com";
            SendSmtpEmailBcc BccData = new SendSmtpEmailBcc(BccEmail, BccName);
            List<SendSmtpEmailBcc> Bcc = new List<SendSmtpEmailBcc>();
            Bcc.Add(BccData);
            string CcName = "John Doe";
            string CcEmail = "example3@example2.com";
            SendSmtpEmailCc CcData = new SendSmtpEmailCc(CcEmail, CcName);
            List<SendSmtpEmailCc> Cc = new List<SendSmtpEmailCc>();
            Cc.Add(CcData);
            string HtmlContent = "<html><body><h1>This is my first transactional email {{params.parameter}}</h1></body></html>";
            string TextContent = null;
            string Subject = "My {{params.subject}}";
            string ReplyToName = "John Doe";
            string ReplyToEmail = "replyto@domain.com";
            SendSmtpEmailReplyTo ReplyTo = new SendSmtpEmailReplyTo(ReplyToEmail, ReplyToName);
            string AttachmentUrl = null;
            string stringInBase64 = "aGVsbG8gdGhpcyBpcyB0ZXN0";
            byte[] Content = System.Convert.FromBase64String(stringInBase64);
            string AttachmentName = "test.txt";
            SendSmtpEmailAttachment AttachmentContent = new SendSmtpEmailAttachment(AttachmentUrl, Content, AttachmentName);
            List<SendSmtpEmailAttachment> Attachment = new List<SendSmtpEmailAttachment>();
            Attachment.Add(AttachmentContent);
            JObject Headers = new JObject();
            Headers.Add("Some-Custom-Name", "unique-id-1234");
            long? TemplateId = null;
            JObject Params = new JObject();
            Params.Add("parameter", "My param value");
            Params.Add("subject", "New Subject");
            List<string> Tags = new List<string>();
            Tags.Add("mytag");
            SendSmtpEmailTo1 smtpEmailTo1 = new SendSmtpEmailTo1(ToEmail, ToName);
            List<SendSmtpEmailTo1> To1 = new List<SendSmtpEmailTo1>();
            To1.Add(smtpEmailTo1);
            Dictionary<string, object> _parmas = new Dictionary<string, object>();
            _parmas.Add("params", Params);
            SendSmtpEmailReplyTo1 ReplyTo1 = new SendSmtpEmailReplyTo1(ReplyToEmail, ReplyToName);
            SendSmtpEmailMessageVersions messageVersion = new SendSmtpEmailMessageVersions(To1, _parmas, Bcc, Cc, ReplyTo1, Subject);
            List<SendSmtpEmailMessageVersions> messageVersiopns = new List<SendSmtpEmailMessageVersions>();
            messageVersiopns.Add(messageVersion);
            try
            {
                var sendSmtpEmail = new SendSmtpEmail(Email, To, Bcc, Cc, HtmlContent, TextContent, Subject, ReplyTo, Attachment, Headers, TemplateId, Params, messageVersiopns, Tags);
                CreateSmtpEmail result = apiInstance.SendTransacEmail(sendSmtpEmail);
                Debug.WriteLine(result.ToJson());
                Console.WriteLine(result.ToJson());
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新