using Microsoft.VisualStudio.TestTools.UnitTesting;
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR.TCC;
namespace Tango.UnitTesting.SendGrid
{
[TestClass]
[TestCategory("SendGrid")]
public class SendGrid_TST
{
private const string KEY = "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";
[TestMethod]
public void Send_Simple_Email()
{
var client = new SendGridClient(KEY);
var from = new EmailAddress("test@example.com", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("roy@twine-s.com", "Roy Ben Shabat");
var plainTextContent = "and easy to do anywhere, even with C#";
var htmlContent = "
and easy to do anywhere, even with C#";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var bytes = File.ReadAllBytes(@"D:\Downloads\Twine TWN Spec (3).pdf");
var file = Convert.ToBase64String(bytes);
msg.AddAttachment("Twine Spec.pdf", file, "application/pdf");
var response = client.SendEmailAsync(msg).GetAwaiter().GetResult();
Assert.IsTrue(response.StatusCode == System.Net.HttpStatusCode.Accepted);
}
[TestMethod]
public void Send_Template_Email()
{
var client = new SendGridClient(KEY);
SendGridMessage msg = new SendGridMessage();
msg.SetFrom("test@example.com");
msg.AddTo("roy.mail.net@gmail.com");
msg.Subject = "SnapMatch Color Result";
msg.SetTemplateId("d-619b8adc604d4f6fa486d7bbc9e3c2cc");
var dynamicTemplateData = new
{
Message = "This is my personal note...",
R = 0,
G = 255,
B = 0,
};
msg.SetTemplateData(dynamicTemplateData);
DetectionColorFile file = new DetectionColorFile();
file.RawColor = new DetectionColor() { R = 10, G = 20, B = 30 };
file.ProcessedColor = new DetectionColor() { R = 11, G = 21, B = 31 };
var base64 = Convert.ToBase64String(file.ToBytes());
msg.AddAttachment("SnapMatch Color.tcc", base64, "application/octet-stream");
var result = client.SendEmailAsync(msg).GetAwaiter().GetResult();
Assert.IsTrue(result.StatusCode == HttpStatusCode.Accepted);
}
}
}