【问题标题】:How to send a iCal invite with Mailgun Rest API (C#)如何使用 Mailgun Rest API (C#) 发送 iCal 邀请
【发布时间】:2016-08-31 13:01:11
【问题描述】:

我正在尝试将 iCal 格式的日历邀请添加到通过 MailGun API 发送的电子邮件中。这是我到目前为止所拥有的:

var request = new RestRequest();

request.AddParameter("domain", this.domain, ParameterType.UrlSegment);
request.Resource = "{domain}/messages";
request.AddParameter("from", contactDetails.SenderAddress);
request.AddParameter("to", contactDetails.RecipientAddress);
request.AddParameter("subject", message.Subject);
request.AddParameter("text", message.TextBody);
request.AddParameter("html", message.HtmlBody);

if (!string.IsNullOrWhiteSpace(message.IcalAttachment))
{
    request.AddFileBytes("attachment", 
                         Encoding.UTF8.GetBytes(message.IcalAttachment), 
                         "invite.ics", 
                         "text/calendar");
}

request.Method = Method.POST;
return request;

这会导致日历作为附件包含在电子邮件中,而不是电子邮件的替代视图。附件在 gmail 中运行良好,但在 Outlook 中显示为附件文件,您必须先单击该附件,然后同意将日历添加到 Outlook 日历。是否有其他方法可以使用 REST api 以便正确发送日历邀请,作为替代电子邮件视图?

明确地说,这就是我使用 .Net SmtpClient 发送日历邀请的方式:

var contentType = new ContentType("text/calendar");
if (contentType.Parameters != null)
{
    contentType.Parameters.Add("method", "REQUEST");
    contentType.CharSet = "UTF-8";
}

// this is the same way you add a html view to the message
request.AlternateViews.Add(
    AlternateView.CreateAlternateViewFromString(
        message.IcalAttachment, 
        contentType));

【问题讨论】:

  • 我遇到了同样的问题。 Mailgun 将 ics 作为附件发送,它仅适用于 SMTP。但我不能使用 SMTP,因为它将电子邮件发送到垃圾邮件。请帮忙。

标签: c# email calendar mailgun


【解决方案1】:

特别感谢 Mailgun 支持为我指明了正确的方向。相关部分或他们的回应是:

您可以使用 /message.mime 端点为日历邀请构建 MIME: https://documentation.mailgun.com/api-sending.html#sending

创建 mime 消息并不像简单地使用 /message 端点那么简单,但是有几个 .net 库可以做到这一点。我在这个例子中使用了MimeKit

var request = new RestRequest();

request.AddParameter("domain", this.domain, ParameterType.UrlSegment);
request.Resource = "{domain}/messages.mime";
request.AddParameter("to", contactDetails.RecipientAddress);
request.AddFile(
    "message", 
    Encoding.UTF8.GetBytes(BuildMimeContent(message)), 
    "message.mime");

request.Method = Method.POST;
return request;

我要创建的 mime 内容将包含一个 multipart/mixed body,它又将包含 multipart/alternative 以及每个附件。日历邀请实际上将附加两次,作为替代视图和附件。这是为了帮助不同电子邮件客户端之间的兼容性。

BuildMimeContent(message) 的实现如下所示:

// create the alternative views
var textBody = new TextPart("plain") { Text = message.TextBody };
var htmlBody = new TextPart("html") { Text = message.HtmlBody };

// add views to the multipart/alternative
var alternative = new Multipart("alternative");
alternative.Add(textBody);
alternative.Add(htmlBody);

if (!string.IsNullOrWhiteSpace(message.CalendarInvite))
{
    // also add the calendar as an alternative view
    // encoded as base64, but 7bit will also work
    var calendarBody = new TextPart("calendar")
    {
        Text = message.CalendarInvite,
        ContentTransferEncoding = ContentEncoding.Base64
    };

    // most clients wont recognise the alternative view without the 
    // method=REQUEST header
    calendarBody.ContentType.Parameters.Add("method", "REQUEST");
    alternative.Add(calendarBody);
}

// create the multipart/mixed that will contain the multipart/alternative
// and all attachments
var multiPart = new Multipart("mixed") { alternative };
if (!string.IsNullOrWhiteSpace(message.CalendarInvite))
{
    // add the calendar as an attachment
    var calAttachment = new MimePart("application", "ics")
    {
        ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
        ContentTransferEncoding = ContentEncoding.Base64,
        FileName = "invite.ics",
        ContentObject = new ContentObject(GenerateStreamFromString(message.CalendarInvite))
    };

    multiPart.Add(calAttachment);
}

// TODO: Add any other attachements to 'multipart' here.

// build final mime message
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(GetMimeAddress(message.MessageInfo.SenderName, message.MessageInfo.SenderAddress));
mimeMessage.To.Add(GetMimeAddress(message.MessageInfo.RecipientName, message.MessageInfo.RecipientAddress));
mimeMessage.Subject = message.Subject;
mimeMessage.Body = multiPart;

// parse and return mime message
return mimeMessage.ToString();

使用 Office 365 进行测试的人员的警告

Office365 在验证日历邀请方面非常挑剔。为了不收到如下消息,您需要确保 vCal 的 organizer 电子邮件地址与电子邮件的 from 地址匹配。如果您使用的是 mailgun 的沙盒测试环境,这是不可能的。

【讨论】:

    猜你喜欢
    • 2019-09-14
    • 2019-06-04
    • 2013-06-09
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多