Results 1 to 9 of 9
  1. #1
    24042014a is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default How to delete sender email address using java

    Hi,

    i found a code to get emails from gmail in this forum.
    It's working very well, now i want to delete a specific sender email address.
    So i can have the sender email with this:

    Java Code:
     String senderAddress = aMessage.getFrom()[0].toString();
     System.out.println("\t From: " + senderAddress);
    Now i want to do something like
    Java Code:
    If (senderAddress = "test@gmail.com"){
    message.setFlag(Flags.Flag.DELETED, true);
    }
    but it's not working.
    Can you help me, thank you

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to delete sender email address using java

    Repeat after me: compare objects (such as Strings) for equality with the equals( ... ) method; not with == or (shudder) =

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to delete sender email address using java

    One problem is the way you are comparing strings. Use equals, not ==, (and definitely not =).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    24042014a is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default Re: How to delete sender email address using java

    Quote Originally Posted by 24042014a View Post
    Hi,

    i found a code to get emails from gmail in this forum.
    It's working very well, now i want to delete a specific sender email address.
    So i can have the sender email with this:

    Java Code:
     String senderAddress = aMessage.getFrom()[0].toString();
     System.out.println("\t From: " + senderAddress);
    String sender = "sender";
    Now i want to do something like
    Java Code:
    If (senderAddress.equals (sender)){
    message.setFlag(Flags.Flag.DELETED, true);
    }
    but it's not working.
    Can you help me, thank you
    Is this right now?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to delete sender email address using java

    Have you tried it? Did it work?

    kind regards,

    Jos

    ps. there is nothing advanced about this; I'm moving this thread.
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    24042014a is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default Re: How to delete sender email address using java

    What i did id:

    if (senderAddress.equals("Test <test@gmail.com>")){
    aMessage.setFlag(Flags.Flag.DELETED, true);
    }
    It just displaying the sender email address but it did not delete it

  7. #7
    24042014a is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default Re: How to delete sender email address using java

    With this, it's working perfectly

    if (message.getSubject().contains("Promo")) {
    message.setFlag(Flags.Flag.DELETED, true);
    }

    so Repeat after me,
    THANK
    YOU

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to delete sender email address using java

    Quote Originally Posted by 24042014a View Post
    With this, it's working perfectly

    if (message.getSubject().contains("Promo")) {
    message.setFlag(Flags.Flag.DELETED, true);
    }

    so Repeat after me,
    THANK
    YOU
    But that is not what you wanted to do!! Deleting based on subject keywords
    is totally unrelated to deleting based on sender address.

    From your earlier post you had this:
    Quote Originally Posted by 24042014a View Post
    What i did id:

    if (senderAddress.equals("Test <test@gmail.com>")){
    aMessage.setFlag(Flags.Flag.DELETED, true);
    }
    It just displaying the sender email address but it did not delete it
    That probably didn't work because your address (at least from the point of the mail
    header rfc) is not an address. Regardless of the text outside of the angle brackets "<>"
    only the user@host part is the actual address and used for delivery.

    So, intuitively, without seeing the mail API, the following should work (I changed equals to
    equalsIgnoreCase since email addresses are case-insensitive).

    Java Code:
    senderAddress.equalsIgnoreCase("test@gmail.com") {
       // do something
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    24042014a is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default Re: How to delete sender email address using java

    I will try it tomorrow.
    You are probably right.
    i need to be at office.
    The solution i found is different and not related with my problem i reckon.
    it just delete the message by looking the subject.

    let me try....
    Thank you

Similar Threads

  1. Using double @ symbol in email address...
    By ravindramg in forum Advanced Java
    Replies: 5
    Last Post: 06-27-2012, 05:51 PM
  2. Validating email address!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2008, 09:39 AM
  3. Validating email address
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 08:42 AM

Tags for this Thread

Posting Permissions

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