2

i use this script from xSnippet to send emails via SSJS http://openntf.org/XSnippets.nsf/snippet.xsp?id=create-html-mails-in-ssjs-using-mime it work greate, but when i have an email address with an special character, it does not work.

Is there any solution to set a charset for the "to" mimeHeader?

var mimeRoot:NotesMIMEEntity = doc.createMIMEEntity("Body");
var mimeHeader:NotesMIMEHeader;

//set to
if (this._to.length>0) {
  mimeHeader = mimeRoot.createHeader("To");
  mimeHeader.setHeaderVal( this._to.join(","));
}
Pudelduscher
  • 359
  • 3
  • 18

3 Answers3

3

Instead of setHeaderVal, try using the addValText() text method, which takes two arguments. The first is the string value, which can be in UTF-8, and the second is the charset -- i.e., "UTF-8". Of course, you need to be sure that your string really is UTF-8. (I'm not familiar with SSJS, so I don't know if it is internally representing your this._to.join(",") value as UTF-8.)

See the doc for the NotesMIMEHeader class for more information about this.

Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
1

As far as I know mail header must contain only US-ASCII characters (this info can be found in MIME specification). Headers with other characters must be encoded. It looks like domino does not encode it by default.

You can try to use javax.mail.internet.MimeUtility.encodeText but this would probably require to get additional jar into project (JavaMail).

W_K
  • 868
  • 4
  • 9
  • The problem is, that you can use UTF-8 characters for mail adresses in the notes-client. I will try the MimeUtility. Thanks – Pudelduscher Mar 05 '13 at 09:05
0

I just tested this due to similar problems and @W_K is right that the MIME headers must not be UTF-8. I ended up doing this which works for the cases that I previously had problems with:

Make sure you MIME encode the email adresses that you later want to add to the TO/CC/BCC MIME headers. I added this to e.g. the setSendTo() method of the emailBean by Tony McGuckin:

import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;

....

public void setSendTo(final String sendTo) {
    try {
        this.sendTo.add(MimeUtility.encodeText(sendTo.replace("[", "").replace("]", ""), "utf-8", null));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

I then use the setHeaderVal() methods to add the required MIME headers but instead of encoding as UTF-8 I just use the default encoding (where getSendTo() just returns the already MIME encoded string):

emailHeader = emailRoot.createHeader("To");
emailHeader.addValText(getSendTo());
Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76