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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 2 of 2

Thread: Javamail

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Javamail

    Good morning to all.

    I'm trying to use javamail for the first time. After the propper Javamail f.A.Q and documentation reading i create a jsp file with a simple email client which is working good except one thing. Every email that i send from my different varius email accounts is a multipart one and contains tow parts with the same message body .
    Even a simple "test" word from outlook,gmail and hotmail is been send in two parts with the same body.
    So in my code i have this java bean which gets the msg and returns the body

    public final String getPart(Part p) throws Exception {		 
     
    		if(p.isMimeType("text/html") || p.isMimeType("text/plain")){
     
    			if(!body.equals((String)p.getContent())){
     
    				logger.info("---- text/plain Message Part ----");
     
    				body = body + (String)p.getContent();}
     
    		}else if(p.isMimeType("multipart/*")){
     
    			logger.info("------- MultiPart Message -------");
     
    			Multipart mp = (Multipart)p.getContent();
     
    			level++;
    			int count = mp.getCount();
     
    			for (int i = 0; i < count; i++)				
    				getPart(mp.getBodyPart(i));
     
    			level--;
     
    		}else if(p.isMimeType("message/rfc822")){
     
    			logger.info("--------- Nested Message --------");
     
    			level++;
     
    			getPart((Part)p.getContent());
     
    			level--;
     
    		}else{
     
    			Object o = p.getContent();
    			if (o instanceof String) {
     
    				logger.info("--------- Simple String ---------");
    				body += (String)o;
     
    			} else if (o instanceof InputStream) {
     
    				logger.info("---------- Inputstream ----------");
     
    			    InputStream is = (InputStream)o;
    			    int c;
    			    while ((c = is.read()) != -1)
    			    	body += c;
     
    			} else {
    				logger.info("---------- Uknown Type ----------");
     
    				body += o.toString();
    			}			
    		}
     
    		String filename = p.getFileName();
    		String disp = p.getDisposition();
     
    		if (level != 0 && disp != null ) {
     
    			if(disp.equalsIgnoreCase(Part.ATTACHMENT)){
     
    		    	if (filename == null)
    		    		filename = "";
     
    		    	try {
    				    File f = new File(filename);
     
    				    if (!f.exists()){
     
    				    	((MimeBodyPart)p).saveFile(f);
     
    				    }
     
    				    body += "Επισυναπτόμενο: <a href=\"" + f.getAbsolutePath() + "\">" + filename + "</a>";
     
    				} catch (IOException ex) {
    				    logger.error("Failed to save attachment: " + ex);
    				}
    			}		    
    		}
     
    		return body;
    	}

    So my problem is that every email is been showing two times without any exception. Any ideas will be appreciated.

    Thanks in advance..


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Javamail

    After a lot of testing i figure out that every message has a content type of "multipart/mixed" or "multipart/alternative". So i will handle those too seperate cause the first contains an attachment and the second is just the mail devided in some text/plain and text/html parts.