HTML messages

Apr 18, 2014 at 5:33 PM
Hi!

I wanted to know how can I deal with HTML messages, very often I find that html messages don't have IsHtml set to true, and instead they've got attachments without name which contain parts of html messages, and I wanted to know, how to be sure that message is or isn't html, and how to get full message in html (if it is possible with Your library )?
Coordinator
Apr 21, 2014 at 1:02 AM
Hi, Jantar!

Can you show me the raw message of mail?
I could not say possible or not without raw data.

Maybe it would be imposibble with current version, but I can add feature that you want in the future version.

reagreds.
Apr 21, 2014 at 12:22 PM
Hi!

Thanks for a reply, I will post raw message later, for now I will tell You how I managed to deal with this problem: I take Contents property of a MailMessage and then for each MailContent in Contents I check if it has IsBody property to true and if it is I add it to "BodyParts" and then recursively I do the same for its Contents, and afterwards from those BodyParts i build Body (html) (but first BodyPart is always plain text message so I have to omit this one ). It sure would be great to have it somehow automated in library, but I'm not sure if what I do is correct, but so far it works.

Regards
Janusz Tarczykowski
Coordinator
Apr 22, 2014 at 3:51 AM
Hi.

I could understand exactly what you want to do if you show me your source code.
Can you show me your source code?
Apr 23, 2014 at 3:17 PM
Here is a method in which I add a Message to the Database. IsHtml is not enough, becasue it works only if BodyText is html, but very often html message is in attachments, not in BodyText

public MessageDTO AddMessageToDatabase(MailMessage mm, string uid, int folderId)
    {
        MessageDTO msg = new MessageDTO();
        msg.Date = mm.Date.DateTime;
        msg.To = mm.To;
        msg.From = mm.From;
        msg.Subject = mm.Subject;
        msg.IsUnread = true;
        msg.FolderID = folderId;
        msg.Uid = uid;
        msg.IsHtml = mm.IsHtml;
        msg.Text = ConvertEncoding(mm.ContentEncoding, Encoding.UTF8, mm.BodyText);
        #region 
        mm.Contents.ForEach(content =>
        {
            var parts = BuildBody(content, msg.Id);
            foreach(var part in parts)
            {
                msg.BodyParts.Add(part);
            }
            if(!content.IsBody)
            {
                MemoryStream stream = new MemoryStream();
                content.DecodeData(stream, false);
                var att = new AttachmentDTO();
                att.MessageDTOID = msg.Id;
                att.FileName = content.FileName;
                att.Content = stream.ToArray();
                msg.Attachments.Add(att);
            }
        });
        #endregion

        using (var db = GetContext())
        {
            db.MessageDTOSet.Add(msg);
            db.SaveChanges();
        }
        return msg;
    }
and here is my BuildBody method:

public IList<AttachmentDTO> BuildBody(MailContent content, int messageId)
    {
        var result = new List<AttachmentDTO>();
        if(content.IsBody)
        {
            MemoryStream stream = new MemoryStream();
            content.DecodeData(stream, false);
            var att = new AttachmentDTO();
            att.MessageDTOID = messageId;
            att.FileName = content.FileName;
            att.Content = stream.ToArray();
            result.Add(att);
        }
        content.Contents.ForEach(con => result.AddRange(BuildBody(con,messageId)));
        return result;
    }
Coordinator
Apr 24, 2014 at 10:42 AM
Hi.

Thank you for your upload your source code.
I understand what you want, I'll add some feature like
var html = mg.GetAllHtmlText();
html has "get full message in html" of MailMessage object.
I will get it by aggregating MailMessage.Contents recursively.
Is it ok?

I must find how to determine whether the mail is html or not.
To do it, please upload some of rawtext of mail.

regards.