将SMTP Sendgrid转换为Sendgrid API

问题描述

当前将旧的SMTP Sendgrid替换为API Sendgrid,我注意到它们的代码有所不同。但是我在想,因为它们都是sendgrid,所以可以正常工作。我要做的是最后添加SendGridClient.SendEmailAsync(Message);。但它说

无法从system.net.mail.mailmessage转换为sendgrid.helpers.mail.sendgridmessage

这是转换它的正确方法吗?

下面是代码

        try
        {
            string SendGridKey = ConfigurationManager.AppSettings["SendGridKey"];
            var SendGridClient = new SendGridClient(SendGridKey);
            using (MailMessage MessageContent = new MailMessage())
            {
                MessageContent.From = new MailAddress(From);
                MessageContent.To.Add(new MailAddress(To));
                MessageContent.Subject = Subject;
                MessageContent.Body = (TextBody);
                ContentType mimeType = new System.Net.Mime.ContentType("text/html");
                AlternateView Alternate = AlternateView.CreatealternateViewFromString(HtmlBody,mimeType);
                Message.AlternateViews.Add(Alternate);
                if (AttachedFileName == true)
                {
                    Attachment AttachedFile = new Attachment(HttpRuntime.AppDomainAppPath + "Path\\" + AttachedFileName);
                    MessageContent.Attachments.Add(AttachedFile);
                }

                //using (SmtpClient Client = new SmtpClient())
                //{
                //    Client.EnableSsl = true;
                //    Client.Send(MessageContent);
                //}
                  SendGridClient.SendEmailAsync(Message);
            }
            return;
        }

解决方法

要使用 SendGrid API 发送邮件,您需要创建 com.sendgrid.Request 对象。为java添加代码:

// using Document
BasicInfo.find(new Document("$or",new Document("uuid",1).append("uuid",2)));
BasicInfo.find(new Document("uuid",new Document("$id",List.of(1,2))));
// using a raw JSON query
BasicInfo.find("{'$or': {'uuid':1,'uuid':2}}");
BasicInfo.find("{'uuid': {'$in': [1,2]}}");
// using Panache QL
BasicInfo.find("uuid in (1,2)");
// or cannot be used in PanacheQL with multiple times the same field name