Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to remove all Headers from a MimeMessage

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Volos Greece
    Thanked 0 Times in 0 Posts

    Default How to remove all Headers from a MimeMessage

    Hello i am having a problem in removing all headers from a Part in java mail.The code i use :

    Enumeration headers = part.getAllHeaders();
           while (headers.hasMoreElements()) {
                    Header h = (Header) headers.nextElement();
                    System.out.println(h.getName() + ": " + h.getValue());

    The System.out.println gives me right names an values!!But when i use part.removeHeader(h.getName()); nothing seems to be removed!!Has anybody any idea where is the problem or suggest me a new way to remove all the Headers??

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: How to remove all Headers from a MimeMessage

    I would recommend not removing elements of Part while iterating over the header contents...you may be changing the underlying data without the Enumerator knowing (I am not familiar with the API so cannot say this for sure, but directly modifying a Collection while iterating over the contents should not be done - in the case of an Iterator often times a ConcurrentModificationException will be thrown). Suggest copying the headers into another local List or Set, then loop over that Collection removing them from the Part one by one.

Similar Threads

  1. [SOLVED] MD5 hash and MimeMessage problem
    By ArgyrisV in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 19th, 2011, 04:28 PM
  2. JTree - Remove All Nodes
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: December 9th, 2010, 05:27 PM
  3. Set headers for HttpServletRequest object?
    By FailMouse in forum Web Frameworks
    Replies: 3
    Last Post: December 1st, 2010, 03:00 PM
  4. Javamail html mail sended as text and headers problem
    By johnymj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 29th, 2010, 09:22 AM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM