Send HTML email with embedded images

This tip describes how to send an e-mail with HTML and embedded images.

This tip describes how to send an e-mail with HTML and embedded images.

import lotus.domino.*;
import java.io.*;
import java.util.*;

public class JavaAgent extends
 AgentBase {

private MIMEEntity mime;
private int byteNumber;
private int count;
private byte byteArray[];
  
 public void NotesMain() {
  PrintWriter pw = getAgentOutput();
  pw.println("<result>");
  try {
   Session curSession = 
getSession();
   AgentContext agentContext = 
curSession.getAgentContext();
   Database curDb = 
agentContext.getCurrentDatabase();
   
   Document contextDoc = 
agentContext.getDocumentContext();
   String qs = contextDoc.
getItemValueString
("Query_String_Decoded");
   int pos = qs.indexOf("=");
   String unid = qs.substring
(pos+1,qs.length());
//   pw.println("Docunid:"+
unid+"</br>");

   Document curDoc = curDb.
getDocumentByUNID(unid);

   String mailBody = 
curDoc.getItemValueString("Body");
//   pw.println("mailBody:"+
mailBody+"</br>");
   String mailSubject = 
curDoc.getItemValueString("subject");
//   pw.println("mailSubject:"+
mailSubject+"</br>");
   String mailSendTo = 
curDoc.getItemValueString("sendTo");
//   pw.println("mailSendTo:"+
mailSendTo+"</br>");
   //Vector vToTrue = curDoc.
getItemValue("SendTo");

   Document mailDoc = 
curDb.createDocument();
   mailDoc.replaceItemValue
("Form", "Memo");
   
   //Creeer multipart
   //"multipart/alternative"
   curSession.setConvertMIME(false); 
// Block conversion
   Stream multiStream = curSession.
createStream();
   multiStream.writeText("Dit is een 
multipart bericht");
   MIMEEntity mimeRoot = 
mailDoc.createMIMEEntity();
   MIMEHeader header = mimeRoot.
createHeader("Content-Transfer-Encoding");

   //mailinfo
   header = mimeRoot.createHeader
("Subject");
   header.setHeaderVal(mailSubject);
   header = mimeRoot.createHeader
("To");
   header.setHeaderVal(mailSendTo);
   mimeRoot.setContentFromText
(multiStream,"multipart/related",
MIMEEntity.ENC_NONE);
   multiStream.close();
  
   //afbeeldingen toevoegen
   /*
   Stream imageStream = curSession.
createStream();
   EmbeddedObject file = curDoc.
getAttachment("closeIcon.gif");
   InputStream fileStream = 
file.getInputStream();
   byteArray = new byte[fileStream.
available()];
   byteNumber = fileStream.read();
   count = 0;
   while(byteNumber > -1){
    Integer byteInteger = new Integer
(byteNumber);
    byteArray[count] = byteInteger.
byteValue();
    byteNumber = fileStream.read();
    count += 1;
   }
   imageStream.write(byteArray);
   mime = mimeRoot.
createChildEntity();
   header = mime.createHeader
("Content-Disposition");
   header.setHeaderVal("inline; 
filename=closeIcon.gif");
   header = mime.createHeader
("Content-ID");
   header.setHeaderVal
("<closeIcon.gif>");
   //header.setHeaderVal("closeIcon.gif");
   header = mime.createHeader
("Content-Transfer-Encoding");
   header.setHeaderVal("Base64");
   mime.setContentFromBytes
(imageStream,"image/gif",
MIMEEntity.ENC_NONE);
   //mime.encodeContent(MIMEEntity.
ENC_BASE64);
   imageStream.close();
   */
   
   //html toevoegen
   Stream htmlStream = curSession.
createStream();
   mime = mimeRoot.createChildEntity();
   htmlStream.writeText(mailBody);
   mime.setContentFromText(htmlStream,
 "text/html;charset=ISO-8859-1", 
MIMEEntity.ENC_NONE);
   htmlStream.close();
 
   //email verzenden
   mailDoc.send(false);
   curSession.setConvertMIME(true); 
// Restore conversion
   pw.println("<status>true
</status>");
   pw.println("<message>Correct 
uitgevoerd</message>");
   
  } catch( Exception e) {
   pw.println("<status>false
</status>");
   pw.println("<message>"+
e.toString()+"</message>");
   e.printStackTrace();
  }
  //einde html output
  pw.println("</result>");
 }
}

Do you have comments on this tip? Let us know.

This was first published in April 2004

Dig deeper on Java for Lotus Notes Domino

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close