• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to properly read the custom Header value in the E-mail envolope

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm writing a java pop3 Client app to Send / Read emails sending invoices to customers. I'm including a custom header with the invoice number as value during the email send. The code snippet for the same is given below:

Message msg = new MimeMessage(session);
msg.setFrom( new InternetAddress(from));
InternetAddress [] address = {new InternetAddress(to)};
InternetAddress [] replyto = {new InternetAddress(from)};
msg.setRecipients(Message.RecipientType.TO, address);
//msg.setSendDate(new Date());
msg.setReplyTo(replyto );
// Header is a string //msg.setHeader("Custom",header);

The email scanning code is given below:

// Create empty properties

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
// Get the store
Store store = session.getStore("pop3");
store.connect(host, username, password);
// Get folder
POP3Folder folder = (POP3Folder) store.getFolder("INBOX");
Message[] msgs = folder.getMessages();
// Use a suitable FetchProfile
FetchProfile fp = new FetchProfile();
folder.fetch(msgs, fp);
Message msg;

for (int i = 0; i < msgs.length; i++)
String uid = folder.getUID(msgs[i]);
log.info("UID :" + uid);
// if the UID is not available
if (! uids.IsAvailable(uid)) {
// Then fetch the message and push it to the array list
Address[] addrs;
Date dt;
// Get subject
String sub = msgs[i].getSubject();
// Get from address
addrs = msgs[i].getFrom();
String from = addrs[0].toString();
// Get send date
dt = msgs[i].getSentDate();
// Get the Header
String [] header = msgs[i].getHeader("Custom");

if (header.length> 0 ) {
log.info("Reached Here 2");
log.info("Header value is " + header[0]);
log.info("Reached Here 3");

log.info("The values are: " + from + "&" + sub);
//Get the content of the message
Part messagePart = msgs[i];
Object content = messagePart.getContent();

// -- or its first body part if it is a multipart message --
if (content instanceof Multipart)
messagePart = ((Multipart)content).getBodyPart(0);
log.info("This is a Multipart Message");
// -- Get the content type --
String contentType = messagePart.getContentType();
log.info("Retrieving the message content");
String msgContent = "";
// -- If the content is plain text, we can print it --
if (contentType.startsWith("text/plain"))
InputStream is = messagePart.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String thisLine = reader.readLine();
while (thisLine != null)

msgContent += thisLine;
thisLine = reader.readLine();

But the Header retrieved is null in the following statement.

// Get the Header String [] header = msgs[i].getHeader("Custom");

Did I miss anything? Thank you for your time.
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would check the value of header in the line:
Symphony Rajiv
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the "header" has a valid value.
    Bookmark Topic Watch Topic
  • New Topic