Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default javamail, getSubject, and if()

    Hey, im new to java but have coded in multiple other languages including objective-c. I'm currently working on a program that gets mail from an smpt server and scans the messages for ones with a certain subject:"Tester" So far, I have the program retrieving the messages and I have a message with the Subject:"Tester", but when I use an if statement to check if the subject is "Tester" it returns false. My code is below. I am wondering if there is any special format for subjects or something?
    Java Code:
     
    public void getRecieptMessage() throws Exception {
            Message m = null;
            
            try {
            	
                for (int i = 1; i <= getMessageCount(); i++) {
                	m = folder.getMessage(i);
    	            System.out.println(m.getSubject());
                	if (m.getSubject() == "Tester"){
        	            System.out.println("found");
        	            printMessage(i);
                	}
                }
            } catch (IndexOutOfBoundsException iex) {
                System.out.println("Message number out of range");
            }
        }
    I have other voids to retrieve certain information ie:getMessageCount()
    but the focus is on the if statement, it just won't return true.

  2. #2
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    write your if statement like.

    if(message.getSubject().equals("Tester"))

    {
    System.out.println("found");
    printMessage(i);
    }

    good luck!!!

  3. #3
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    When checking for equality of String objects, always use the .equals methods.

    Best,
    --user0--

  4. #4
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks guys so much! Iv'e seen then before in a few hey0 plugins for minecraft and totally forgot about it.

  5. #5
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default Adress[]

    One other thing. Say next I want to test the from address. I want to see if its from "usr@email.com" how would i go about doing this? I know there is a class:"Address[]" but im not sure how to set it to a certain email adress. I'm hoping the code will look something like this:
    Java Code:
    Address[] address = "usr@email.com"; //thats the part i need help with
    if(m.getAddress().equals(address)){
    	            System.out.println("found");
    	            printMessage(i);
    	            }

  6. #6
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    you can use

    InternetAddress[] add = InternetAddress.parse("xyz@mail.com");

    add1=(InternetAddress[]) message.getFrom();

    for(InternetAddress a:add)
    {
    for(InternetAddress b:add1)
    {
    if(a.equals(b))
    {
    System.out.println("found");
    printMessage(i);
    }
    }
    }

    good luck!!!

Similar Threads

  1. Help with javamail ??
    By nikunj in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-02-2011, 12:12 PM
  2. Javamail
    By johniem in forum New To Java
    Replies: 0
    Last Post: 07-14-2010, 03:00 PM
  3. JavaMail API
    By eponcedeleon in forum Advanced Java
    Replies: 1
    Last Post: 02-28-2010, 12:39 AM
  4. Javamail
    By johniem in forum New To Java
    Replies: 1
    Last Post: 01-29-2010, 03:24 PM
  5. JavaMail Jar
    By rummy in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2010, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •