问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/37619992/squeak-securesmtpclient-to-send-html-email