1

I am having trouble figuring out how to send an html e-Mail using SecureSMTPClient. The e-Mail client always shows it as plaintext, which means that the MIME is not getting set, i.e. SecureSMTPClient forgets to send:

MIME-Version: 1.0
Content-Type: text/html; charset=utf-8

eventhough I specify text/html.

Here is the code

title:='title1'.
content:='<a href="myurl">a link</a>'.

smtpClient := SecureSMTPClient new.
smtpClient user: senderMailAddress.
smtpClient password: pw.
smtpClient openOnHost: (NetNameResolver addressForName: 'smtp.gmail.com') port: 465.

message := MailMessage empty.
message setField: 'from' toString: senderMailAddress.
message setField: 'to' toString: rcvrAddress.
message setField: 'subject' toString: title.
msgBody:= MIMEDocument contentType: 'text/html' content: content.
message body: msgBody.

smtpClient mailFrom: senderMailAddress to: {rcvrAddress} text: message text.
smtpClient quit.

This question is related, however the answer depends on a class available in Pharo but not Squeak.

Community
  • 1
  • 1
Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66

1 Answers1

3

I believe that you've hit an unmaintained part of the system. As far as I can tell you're supposed to used #bodyTextFormatted instead of #text in your example. For the html MIME type that method would then generate the correct body. Unfortunately, HtmlParser, which is used there, is not part of the image and none of the older versions I could find work with MailMessage (i.e. an exception is produced one way or another). I'm therefore not sure that the body will be encoded correctly.

Apart from that, you can just set additional headers like you already did in your example:

message
    setField: 'content-type' toString: 'text/html; charset=utf8';
    setField: 'mime-version' toString: '1.0'.

So give it a try with these additional headers.

Max Leske
  • 5,007
  • 6
  • 42
  • 54
  • Setting those fields explicitly worked great. (For future visitors: I had tried #bodyTextFormatted before, and it returns the message as body-only, i.e. no e-Mail-Title.) – Bernd Elkemann Jun 04 '16 at 11:19
  • True, I forgot to mention that. The reason for this is that the `HtmlParser` is not part of the system and the fallback is to use `#text` like you did anyway. – Max Leske Jun 04 '16 at 11:32
  • You helped me a lot with this answer. Now I have posted another similar question. If you are interested: http://stackoverflow.com/questions/38049364/squeak-smtps-on-linux – Bernd Elkemann Jun 27 '16 at 08:42