本文介绍了SendGrid SMTP API:嵌入图片:坏请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是sendgrid SMTP API https://github.com/sendgrid/sendgrid-csharp发送电子邮件,但我无法弄清楚如何嵌入图像。我可以使用原生的.Net API的邮件没有问题,做到这一点。我只是得到一个错误的请求。这里是我的code,它是扔

 私有静态无效的主要(字串[] args)
    {
        尝试{
           ////首先创建电子邮件对象,那么添加的属性。
            VAR myMessage =新SendGridMessage();            CONTACT_LIST =新的List< MailAddress>();
            contact_list.Add(新MailAddress(
[email protected]));
            myMessage.To = contact_list.ToArray();
            myMessage.From =新的MailAddress([email protected]);
            myMessage.Subject =主题;            字符串的html =< D​​IV>< IMG SRC = CID:标志/>< / DIV>中;            myMessage.Html = HTML;
            myMessage.EmbedImage(@C:\\ logo.png,标志);            SendMessage函数(myMessage);
        }
        赶上(例外五)
        {
            Console.WriteLine(e.Message);
        }    }    私有静态无效的SendMessage(SendGridMessage消息)
    {
        //创建凭据,指定您的用户名和密码。
        VAR证书=新的NetworkCredential(用户名,pwdpwdpwd);        //创建用于发送电子邮件一个Web传输。
        VAR transportWeb =新的Web(凭证);        //发送电子邮件。
        尝试
        {
            transportWeb.Deliver(消息);
            Console.WriteLine(已发送!);
        }
        赶上(异常前)
        {
            Console.WriteLine(ex.Message);
        }
    }


解决方案

我想出如何得到这个工作。下面是使用code:

  VAR myMessage =新SendGridMessage();CONTACT_LIST =新的List< MailAddress>();
contact_list.Add(新MailAddress(我的电子邮件地址));myMessage.To = contact_list.ToArray();
myMessage.From =新的MailAddress([email protected],STP客户服务);
myMessage.Subject =STP报告数据包:+ package_report_name;
字符串IMG = @C:\\\\ logo.png
ContentType的CTYPE =新的ContentType(图像/ PNG);
VAR附件=新的附件(IMG,CTYPE);
VAR linkedResource =新LinkedResource(IMG,CTYPE);
myMessage.AddAttachment(attachment.ContentStream,attachment.Name);
myMessage.EmbedImage(attachment.Name,linkedResource.ContentId);字符串的html =< IMG SRC = CID:+ linkedResource.ContentId +/>中;
myMessage.Html = HTML;

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 it using .Net native mail api without issues. I am simply getting a Bad Request. Here is my code that is throwing

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 protected]"));
            myMessage.To = contact_list.ToArray();
            myMessage.From = new MailAddress("[email protected]");
            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);
        }
    }
解决方案

I figured out how to get this working. Here is the code used:

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("[email protected]", "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;

这篇关于SendGrid SMTP API:嵌入图片:坏请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:20