Search code examples
c#mime

How to Send a MimeMessage


May I know how can I send out a MimeMessage?

Below is my code snippet:

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;
}

Edit: 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...


Solution

  • 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);
    }