Tip

Send HTML email with 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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.