首页 > 解决方案 > 使用 SendGrid C# 在电子邮件正文中渲染 SVG

问题描述

我必须在电子邮件正文中放置一个简单的图像以及超链接。要发送电子邮件,我使用的是 SendGrid API。

我的 C# 代码:

public async Task SendEmailAsync(EmailDeliveryModel model) {
    var response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
    var client = new SendGridClient(ConfigurationManager.AppSettings[CommonObject.SendGridKey]);
    var msg = new SendGridMessage();
    msg.SetFrom(new EmailAddress(model.SourceEmail));
    var recipients = new List();
    //multiple recipients
    foreach(string emailAddress in model.DestinationEmail.Trim(',').Split(',')) {
        recipients.Add(new EmailAddress(emailAddress.Trim()));
    }
    msg.AddTos(recipients);
    msg.SetSubject(model.Subject);
    if (model.ContentType.Equals(CommonObject.ContHTML, StringComparison.OrdinalIgnoreCase)) {
        msg.AddContent(MimeType.Html, model.EmailContent + "\n \n \n \n \n");
    } else {
        msg.AddContent(MimeType.Html, model.EmailContent + "\n \n \n \n \n");
    }
    var sendGridresponse = await client.SendEmailAsync(msg);
    if (sendGridresponse.StatusCode == System.Net.HttpStatusCode.Accepted) {
        response.StatusCode = System.Net.HttpStatusCode.OK;
    }
    return response;
}

在 EmailContent 中,我将 html 内容与 SVG 一起注入,但 SVG 并未在电子邮件中呈现。但是,如果我在浏览器中尝试相同的 html,它就可以工作。

如何在电子邮件正文中发送图像以及超链接?

标签: c#.netsendgrid

解决方案


推荐阅读