Archived Forum Post

Index of archived forum posts


SMTP: If the first attachment is HTML, it will be empty.

Oct 23 '17 at 02:45

If I send a mail through Chilkat SMTP (ActiveX) and the first attachment is an HTML file, the file will contain the body of the e-mail instead of the actual attached HTML file.

The body of the e-mail itself will be empty as well.

If I put another type of file (like a PDF) first, the HTML files that follow will be correct and the body will be correct as well.

See pictures below:

This is when the HTML attachment is first in the list:


This is when the HTML is second or later in the list:


UPDATE: The body of the mail will replace the first HTML attachment...

So for example, if the body contains the text 'aaaaa', the HTML attachment (if it's the first attachment in the list) will only contain 'aaaaa' instead of the actual attached HTML.

Accepted Answer

Setting the HTML body of the email first, and then adding attachments (such as an HTML file attachment) should solve this problem.

The issue occurs when you have an empty/new email object, and the very first thing you do is call email.AddFileAttachment to add a file that happens to be an HTML file.

It's somewhat of a sticky situation because Chilkat is handling the auto-formatting of the MIME structure (i.e. multipart/mixed, multipart/related, multipart/alternative) and re-arranging things automatically as parts are added via different method calls -- such as adding a related item, adding a plain-text body after an HTML body is already present, adding attachments, etc.

An email that has file attachments is typically multipart/mixed. But there is the case where an email has no actual body, and just one file attachment. In that case, the email should NOT be multipart/mixed, but should just be the simple non-multipart MIME containing the file attachment. This is what happens when an HTML file attachment is added first. You get an email with MIME that is non-multipart, and the top-level Content-Type is text/html -- basically indistinguishable from an email where SetHtmlBody was first called. Then.. if you call SetHtmlBody, the software replaces the HTML body.

But if you reverse the calls, the semantics of AddFileAttachment are to convert the email to multipart/mixed if a body already exists, and then add the file attachment under the multipart/mixed umbrella. This is why calling SetHtmlBody first, and then calling AddFileAttachment works, but the reverse does not.