logo

Java Class For Sending HTML Emails from Lotus Domino

Remember the LotusScript Email class from last October? I've been using it ever since, as it makes sending emails so much easier. You should consider using it - or something similar - if you send lots of emails.

Yesterday I had need to send an email from a Java agent and found myself wishing I had a similar Java class, so I spent half an hour porting the LotusScript to Java. Here's the resulting code, which you can import it to your own agents.

Use it like this:

Email mail = new Email();
mail.setSubject("Test Email");
mail.setHTMLPart("<p>this is <b>html</b>!</p>");
mail.setTextPart("this is just text!"); mail.send("jake howlett");

As with the LotusScript class you can choose to omit the text part if you wish (bad practice to though!), add styles to the HTML etc.

Thought I'd post it here in case anybody else might like it. Or maybe you can offer advice on how to improve it. One thing I need to do today is add an addFileAttachment() method.

Comments

    • avatar
    • Steve M
    • Fri 8 Jan 2010 07:57 AM

    Hi Jake

    I have done something similar to addFileAttachment() in my own email class.

    I was actually creating the attachment to order at run time rather attaching an existing one, so it may not fit your needs but in case it is of use or can be modified I'll explain the idea behind it :

    In the class, the various MIME headers only get put together at the time the 'send' method is called. By this point I've already got my HTML text stored as a stream and the text I want to go in the attachment in another stream. Both streams are properties of the email object. I have an additional string property of the object that has the desired name of the file attachment (textAttachFileName).

    The 'send' method sets the MIME headers for the text & HTML parts in the usual way but then creates a new header and child entity that specifically represent the attachment. In the below code the "textAttachStream" can be any text stream you want and is used as the content of the attachment - you may be able to change this bit to write a byte stream instead if your attachment already exists.

    Hope that makes sense - happy to send more details if needed.

    Thanks for a fantastic blog by the way.

    Steve

    '########################

    'A new child mime entity to hold a file attachment

    Dim header As NotesMIMEHeader

    Dim bodyChild As NotesMIMEEntity

    Set bodyChild = body.CreateChildEntity()

    Set header = bodyChild.createHeader("Content-Type")

    Call header.setHeaderVal("multipart/mixed")

    Set header = bodyChild.createHeader("Content-Disposition")

    Call header.setHeaderVal("attachment; filename=" & textAttachFileName )

    Set header = bodyChild.createHeader("Content-ID")

    Call header.setHeaderVal( textAttachFileName )

    Call bodyChild.SetContentFromText( textAttachStream, textAttachMimeType, ENC_IDENTITY_7BIT)

    '########################

      • avatar
      • Jake Howlett
      • Fri 8 Jan 2010 08:09 AM

      Hi Steve. Funnily enough that's pretty much exactly what I did after blogging the above. I now have a method of my Email class to add a file.

      One thing I did find is that I needed to have a root MIME entity that was "multipart/mixed", which contained the file as well as a child MIME entity of type "multipart/alternative", which contained the text and HTML of the message.

      Without the parent "mixed" entity it would work in Notes client but not Thunderbird (and I guess others)

      Jake

    • avatar
    • Jyri
    • Tue 31 Aug 2010 06:41 AM

    Hi Jake,

    It would be nice to see how you solved adding attachment to HTML email (using Java).

    Jyri

    • avatar
    • Jie Cerbo
    • Sun 14 Aug 2011 04:35 AM

    I know this is an old thread but I hope somebody can still help me.

    I have done something similar in C#. However I'm encountering an issue when i am attaching multiple files with different file types. For example, I am attaching an excel file and a word file. When the mail is received in lotus notes client only the first attachment can be opened, the second attachment gives an error that the file is in a different format. Following is the code i'm using:

    ********

    public static void sendLotusFormat()

    {

    Domino.NotesSession session = new Domino.NotesSession();

    string password = "<lotusmailpassword>";

    session.Initialize(password);

    object itemvalue = null;

    String mailServer = ConfigurationManager.AppSettings["lotusmailserver"];

    String mailFile = "<path to names.nsf>";

    Domino.NotesDatabase email = session.GetDatabase(mailServer, mailFile, false);

    if (!email.IsOpen) email.Open();

    Domino.NotesDocument memo = email.CreateDocument();

    memo.ReplaceItemValue("Form", "Memo");

    memo.ReplaceItemValue("Subject","Subject");

    string mess = someHTMLformattedmessage;

    memo.ReplaceItemValue("SendTo", mailaddress);

    memo.SaveMessageOnSend = true;

    itemvalue = memo.GetItemValue("SendTo");

    session.ConvertMime = false;

    Domino.NotesStream stream = session.CreateStream();

    stream.WriteText(mess, Domino.EOL_TYPE.EOL_ANY);

    Domino.NotesMIMEEntity mimeBody = memo.CreateMIMEEntity("Body");

    Domino.NotesMIMEEntity mimeHtml = mimeBody.CreateChildEntity(null);

    Domino.NotesMIMEHeader header = mimeBody.CreateHeader("Subject");

    //For the HTML part of the message

    mimeHtml.SetContentFromText(stream, "text/html; charset=\"iso-8859-1\"", Domino.MIME_ENCODING.ENC_QUOTED_PRINTABLE);

    stream.Close();

    stream.Truncate();

    //This is for looping through an arraylist with names of the files to be attached

    if (file.Count > 0)

    {

    String fileName = "";

    foreach (string i in file)

    {

    fileName =i.Substring(i.LastIndexOf("\\") + 1);

    mimeHtml = mimeBody.CreateChildEntity(null);

    header = mimeHtml.CreateHeader("Content-Disposition");

    header.SetHeaderVal("attachment; filename=" + fileName);

    header = mimeHtml.CreateHeader("Content-ID");

    header.SetHeaderVal(fileName);

    stream = session.CreateStream();

    if (stream.Open(i, "binary"))

    {

    mimeHtml.SetContentFromBytes(stream, "application/octet-stream", Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);

    mimeHtml.EncodeContent(MIME_ENCODING.ENC_BASE64);

    }

    stream.Close();

    stream.Truncate();

    }

    }

    memo.Send(false, ref itemvalue);

    memo.Save(true, false, false);

    stream.Close();

    session.ConvertMime = true;

    memo.CloseMIMEEntities(true, "Body");

    memo.ComputeWithForm(true, false);

    }

    ********

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Fri 8 Jan 2010

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content