SendGrid SMTP API: Inline Image: Bad Request

I am using the sendgrid SMTP API https://github.com/sendgrid/sendgrid-csharp to send emails, but I cannot figure out how to embed an image. I can do this using the .Net native mail api without any problems. I just get a bad request. Here is my code that throws

private static void Main(string[] args)
    {
        try {
           //// Create the email object first, then add the properties.
            var myMessage = new SendGridMessage();

            contact_list = new List<MailAddress>();
            contact_list.Add(new MailAddress("email@gmail.com"));
            myMessage.To = contact_list.ToArray();
            myMessage.From = new MailAddress("clientservice@stpis.com");
            myMessage.Subject = "Subject";

            string html = "<div><img src=cid:Logo /></div>";

            myMessage.Html = html;
            myMessage.EmbedImage(@"C:\logo.png", "Logo");

            SendMessage(myMessage);
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }

    }

    private static void SendMessage(SendGridMessage message)
    {
        // Create credentials, specifying your user name and password.
        var credentials = new NetworkCredential("username", "pwdpwdpwd");

        // Create a Web transport for sending email.
        var transportWeb = new Web(credentials);

        // Send the email.
        try
        {
            transportWeb.Deliver(message);
            Console.WriteLine("Sent!");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
+4
source share
2 answers

I figured out how to do it. Here is the code:

var myMessage = new SendGridMessage();

contact_list = new List<MailAddress>();
contact_list.Add(new MailAddress("my email address"));

myMessage.To = contact_list.ToArray();
myMessage.From = new MailAddress("clientservice@stpis.com", "STP Client Service");
myMessage.Subject = "STP Report Package: " + package_report_name;
string img = @"C:\\logo.png";
ContentType ctype = new ContentType("image/png");
var attachment = new Attachment(img, ctype);
var linkedResource = new LinkedResource(img, ctype);
myMessage.AddAttachment(attachment.ContentStream, attachment.Name);
myMessage.EmbedImage(attachment.Name, linkedResource.ContentId);

string html = "<img src=cid:"+linkedResource.ContentId+" />";
myMessage.Html = html;
+7
source

no need to create ContentType and LinkedResource objects. Add the embedded image as an attachment. Also, the EmbedImage method expects the name of the image, not the full path.

:

var myMessage = new SendGridMessage();

        contact_list = new List<MailAddress>();
        contact_list.Add(new MailAddress("email@gmail.com"));
        myMessage.To = contact_list.ToArray();
        myMessage.From = new MailAddress("clientservice@stpis.com");
        myMessage.Subject = "Subject";

        string html = "<div><img src=cid:Logo /></div>";

        myMessage.Html = html;
        **myMessage.AddAttachment(@"C:\logo.png");
        myMessage.EmbedImage("logo.png", "Logo");**

        SendMessage(myMessage);
+3

All Articles