通过mailkit(smtp)发送带有先前消息的电子邮件

时间:2019-05-07 11:38:06

标签: c# sql-server smtp mailkit

我的应用程序是Windows服务,可从邮箱发送和获取消息。电子邮件保存在SQL Server数据库中。现在,我的应用程序发送实际的text / html(发送邮件中不包含以前的电子邮件文本)。我想增加发送带有以前电子邮件text / html的电子邮件的可能性。 mailkit或smtp是否具有可以帮助我实现此机制的功能,还是我应该自己收集有关每封电子邮件的数据并自行构造text / html。

我想通过这种方式发送电子邮件

现在我以这种方式发送电子邮件

这是我发送电子邮件的示例:

var mimeMessage = new MimeMessage();
var multipart = new Multipart("mixed");

mimeMessage.To.AddRange(GetListOfMailboxAddresses(row.Field<int?>("ToGroup")) ?? new List<MailboxAddress>());
mimeMessage.Cc.AddRange(GetListOfMailboxAddresses(row.Field<int?>("CcGroup")) ?? new List<MailboxAddress>());
mimeMessage.Bcc.AddRange(GetListOfMailboxAddresses(row.Field<int?>("BccGroup")) ?? new List<MailboxAddress>());
mimeMessage.From.Add(new MailboxAddress(row["FromName"].ToString(), PluginHelper.ConfigurationXML.SmtpKonfig.SmtpUzytkownik));
mimeMessage.Sender = new MailboxAddress(row["FromName"].ToString(), PluginHelper.ConfigurationXML.SmtpKonfig.SmtpUzytkownik);

mimeMessage.Subject = row["Subject"].ToString();
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = row["HTML"].ToString();
bodyBuilder.TextBody = row["Text"].ToString();
multipart.Add(bodyBuilder.ToMessageBody());
var attachment = new MimePart();

if (Convert.ToInt32(row["SendingEmailId"]) == (int)EmailSendingType.Reply)
{
    if (!mimeMessage.Subject.StartsWith("Re:", StringComparison.OrdinalIgnoreCase))
        mimeMessage.Subject = "Re:" + mimeMessage.Subject;

    ConstructReplyReferences(DataAccess.GetGuidsForReply(Convert.ToInt32(row["ConversationId"]), Convert.ToInt32(row["Id"])), row["ReplyToGuid"].ToString(), mimeMessage);
    try
    {
        ConstructReplyReferencesFromCREW(row.Field<int?>("ObjectNumber"), mimeMessage);
    }
    catch (Exception ex)
    {
        LoggerHelper.LogInfo(ex.ToString());
    }
}

if (!string.IsNullOrWhiteSpace(row.Field<string>("ReplyEmailInUserWindow")))
{
    mimeMessage.ReplyTo.Clear();
    mimeMessage.ReplyTo.Add(new MailboxAddress(row.Field<string>("ReplyEmailInUserWindow")));
}

GetAddAttachments(multipart, Convert.ToInt32(row["Id"]));

mimeMessage.Body = multipart;

client.Send(mimeMessage);

0 个答案:

没有答案