【问题标题】:Lumisoft.net IMAP server can't add attachments to messagesLumisoft.net IMAP 服务器无法向邮件添加附件
【发布时间】:2014-09-17 10:26:51
【问题描述】:

我正在尝试设置从 SQL 数据库中提取数据的 IMAP 服务器。 我的消息正常工作,但我不知道如何将附件附加到它们。

Mail_Message 对象上的附件对象也只有一个 getter 和一个名为 GetAttachments() 的方法,它似乎没有连接到任何地方。

我当前的代码是:

//this is my own object I'm using to pull data from the database
var cmMsg = _ml.GetMessage(mId, session.AuthenticatedUserIdentity.Name, -1);

var msgBody = new MIME_b_Text(MIME_MediaTypes.Text.html);
var msg = new Mail_Message();
msg.Body = msgBody;
msg.To = new Mail_t_AddressList();
msg.From = new Mail_t_MailboxList {new Mail_t_Mailbox(cmMsg.From, cmMsg.FromEmail)};
msg.Cc = new Mail_t_AddressList();
msg.Bcc = new Mail_t_AddressList();

foreach (var recipient in cmMsg.Recipients)
{
    if (recipient.isTo)
    {
        msg.To.Add(new Mail_t_Mailbox(recipient.FullName, recipient.SMTPAddress));
    }
    else if(recipient.isCC)
    {
        msg.Cc.Add(new Mail_t_Mailbox(recipient.FullName, recipient.SMTPAddress));
    }
    else if (recipient.isBCC)
    {
        msg.Bcc.Add(new Mail_t_Mailbox(recipient.FullName, recipient.SMTPAddress));
    }
}

//I tried adding a setter to the attachment object, but just get errors with this code
var a = new List<MIME_Entity>();

foreach (var attachment in cmMsg.Attachments)
{
    var aCT = new MIME_b_Multipart(new MIME_h_ContentType("application/octet-stream"));


    a.Add(new MIME_Entity
    {
        Body = aCT,
        ContentDisposition = new MIME_h_ContentDisposition("attachment"),

    });
}
msg.Attachments = a.ToArray();

msg.Subject = cmMsg.Subject;
msg.Date = cmMsg.TimeDate;
msg.MessageID = cmMsg.InternetMessageId;

if (e.FetchDataType == IMAP_Fetch_DataType.MessageStructure)
{

}
else if(e.FetchDataType == IMAP_Fetch_DataType.MessageHeader)
{

}
else
{
    msgBody.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, cmMsg.HtmlBody);
    _da.MarkAsRead(archiveID, session.AuthenticatedUserIdentity.Name);
}

e.AddData(info, msg);

我不确定我是遗漏了什么还是只是设置错误。我注意到示例项目中有一个 MySQL API,但这也与其中的附件无关。

【问题讨论】:

    标签: c# imap email-attachments lumisoft


    【解决方案1】:

    好吧,事实证明我的做法完全错误。以下是所需的代码

    实际上,它需要将消息设置为多部分混合,而不仅仅是文本。然后,您可以将附件添加为正文部分。

    var cmMsg = _ml.GetMessage(mId, session.AuthenticatedUserIdentity.Name, -1);
    
                MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);
                contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
                MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);
                var msg = new Mail_Message();
                msg.To = new Mail_t_AddressList();
                msg.From = new Mail_t_MailboxList {new Mail_t_Mailbox(cmMsg.From, cmMsg.FromEmail)};
                msg.Cc = new Mail_t_AddressList();
                msg.Bcc = new Mail_t_AddressList();
                msg.Body = multipartMixed;
    
                foreach (var recipient in cmMsg.Recipients)
                {
                    if (recipient.isTo)
                    {
                        msg.To.Add(new Mail_t_Mailbox(recipient.FullName, recipient.SMTPAddress));
                    }
                    else if(recipient.isCC)
                    {
                        msg.Cc.Add(new Mail_t_Mailbox(recipient.FullName, recipient.SMTPAddress));
                    }
                    else if (recipient.isBCC)
                    {
                        msg.Bcc.Add(new Mail_t_Mailbox(recipient.FullName, recipient.SMTPAddress));
                    }
                }
    
                msg.Subject = cmMsg.Subject;
                msg.Date = cmMsg.TimeDate;
                msg.MessageID = cmMsg.InternetMessageId;
                msg.MimeVersion = "1.0";
                msg.Header.Add(new MIME_h_Unstructured("X-MS-Has-Attach", "yes"));
    
                if (e.FetchDataType == IMAP_Fetch_DataType.MessageStructure)
                {
    
                }
                else if(e.FetchDataType == IMAP_Fetch_DataType.MessageHeader)
                {
    
                }
                else
                {
                    MIME_Entity entity_text_plain = new MIME_Entity();
                    MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain);
                    entity_text_plain.Body = text_plain;
                    text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, cmMsg.HtmlBody);
                    multipartMixed.BodyParts.Add(entity_text_plain);
    
    
                    foreach (var attachment in cmMsg.Attachments)
                    {
                        using (var fs = new FileStream(@"C:\test.txt", FileMode.Open))
                        {
                            multipartMixed.BodyParts.Add(Mail_Message.CreateAttachment(fs, "test.txt"));
                        }
                    }
                }
    
                e.AddData(info, msg);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2012-08-08
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多