使用SendGrid C#

时间:2018-06-22 14:37:04

标签: c# .net sendgrid

我必须在电子邮件正文中放置一个简单的图像以及超链接。要发送电子邮件,我正在使用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,它将起作用。

如何在电子邮件正文中连同超链接一起发送图像?

0 个答案:

没有答案
相关问题