如何发送 MimeMessage [英] How to Send a MimeMessage

查看:161
本文介绍了如何发送 MimeMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以知道如何发送 MimeMessage 吗?

May I know how can I send out a MimeMessage?

以下是我的代码片段:

MimeMessage eml = MimeMessage.Load(savedEmlFullFilePath);
MimeMessage toSend = Reply(eml,true); //to send out this message

public static MimeMessage Reply(MimeMessage message, bool replyToAll)
{
    var reply = new MimeMessage();

    // reply to the sender of the message
    if (message.ReplyTo.Count > 0)
    {
        reply.To.AddRange(message.ReplyTo);
    }
    else if (message.From.Count > 0)
    {
        reply.To.AddRange(message.From);
    }
    else if (message.Sender != null)
    {
        reply.To.Add(message.Sender);
    }

    if (replyToAll)
    {
        reply.To.AddRange(message.To);
        reply.Cc.AddRange(message.Cc);
    }

    // set the reply subject
    if (!message.Subject.StartsWith("Re:", StringComparison.OrdinalIgnoreCase))
        reply.Subject = "Re:" + message.Subject;
    else
        reply.Subject = message.Subject;

    // construct the In-Reply-To and References headers
    if (!string.IsNullOrEmpty(message.MessageId))
    {
        reply.InReplyTo = message.MessageId;
        foreach (var id in message.References)
            reply.References.Add(id);
        reply.References.Add(message.MessageId);
    }

    // quote the original message text
    using (var quoted = new StringWriter())
    {
        var sender = message.Sender ?? message.From.Mailboxes.FirstOrDefault();

        quoted.WriteLine("On {0}, {1} wrote:", message.Date.ToString("f"), !string.IsNullOrEmpty(sender.Name) ? sender.Name : sender.Address);
        using (var reader = new StringReader(message.TextBody))
        {
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                quoted.Write("> ");
                quoted.WriteLine(line);
            }
        }

        reply.Body = new TextPart("plain")
        {
            Text = quoted.ToString()
        };
    }

    return reply;
}

还想知道是否可以将 MimeMessage 解析为 EmailMessage 我可以使用 EWS<作为 ResponseMessage 将其发送/代码>...

Would also like to know if it is possible to parse in a MimeMessage into an EmailMessage to I can send it as a ResponseMessage using EWS...

推荐答案

来自 MailKit README:

From the MailKit README:

using (var client = new SmtpClient ()) {
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s,c,h,e) => true;

    client.Connect ("smtp.server.com", 587, false);

    // Note: only needed if the SMTP server requires authentication
    client.Authenticate ("username", "password");

    client.Send (message);
    client.Disconnect (true);
}

这篇关于如何发送 MimeMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆