Getting email of sender: reliable code

I am trying to get a reliable way to find the email address of the sender into a string. The code below will get me the email address that I want, but it seems like a lot of work to do just that. I am surprised that their is not a message.getFromAddress() or message.getEmailAddress(), etc. Let me know if you think that this is a reliable way to get the sender email address. The code works for all instances that I have tried it on.
Address[] addressArray = message.getFrom();
Address someAddress = addressArray[0];
String someAddressString = someAddress.toString();
String email = findEmail(someAddressString);
public static String findEmail(String email) {     
String validEmailReg = "[^A-Za-z0-9\\.\\@_\\-~#]+";
Pattern arrayString = Pattern.compile("[,\\s]+");
          Pattern badChar = Pattern.compile(validEmailReg);
          Pattern brackets = Pattern.compile("<");
          StringBuffer sb = new StringBuffer();
          boolean deletedIllegalChars = false;
     // Split input with the pattern.
          String[] result = arrayString.split(email);
          for (int i = 0; i < result.length; i++) {
               Matcher bracketsM = brackets.matcher(result);
               // try and find the <[email protected]>
               if (bracketsM.find()) {
                    bracketsM = badChar.matcher(result[i]);
                    boolean badCharResult = bracketsM.find();
                    // got the email address. No remove the <>
                    while (badCharResult) {
                         deletedIllegalChars = true;
                         bracketsM.appendReplacement(sb, "");
                         badCharResult = bracketsM.find();
                    bracketsM.appendTail(sb);
                    email = sb.toString();
          return email;

The easy way to do this is to pass the header to an InternetHeaders object. You then have methods like getAllHeaders() to return an Enumeration of the headers and getHeader(String name) to retrieve the value you want.

Similar Messages

  • Cant get emails to send from from ipad mini they just go to outbox and say there is a server error

    I cannot get my emails to send from my ipad mini (neither can orange), they just go to the outbox and say the server will not accept them.  I can receive them ok but not send them.  I have two accounts set up i cloud and orange, this is my main account.

    Double check your outgoing mail server.. settings - mail - account

  • How to get email to send after changing msn password?  Going to settings-mail- enter id

    Very frustrated &amp; this is second attempt for help. Changed my MSN password 2 days ago &amp; now emails stuck in outbox.
    Tried going to settings-email-reenter account name &amp; put in new password. Didn't work. Tried turning phone off to refresh &amp; repeated
    Still will not send.

    If you have mail stuck in the outbox, the only solution I know is to delete the account and then readd it.

  • .mac server down cant get email or send email

    whats going on my .mac says its unavalible , cant check email or sign on ?
    its been like this for 24hours now?

    Hello and welcome to the Discussions.
    I have not had any problem accessing .Mac, or my .mac mail in either Mail program or Browser.
    More info, please, including whether other internet access is functional, in all respects?
    Ernie

  • A friend uses Firefox and the other day it unexpectedly popped up and said it was making some changes. From then till now, he is unable to get email or send email or access the internet. Is there a virus or a fake Firefox look alike? What should he do?

    His computer is not working since Firefox updated it (in the middle of when he was trying to do somethine else) Help

    His computer is not working since Firefox updated it (in the middle of when he was trying to do somethine else) Help

  • Can't get mail to send via an ethernet broadband modem

    Just changed to Sky Broadband. Internet access and downloading emails works fine, but can't work out how to get emails to send without using my old dial-up connection. Help!
      Mac OS X (10.3.9)  

    One thing you can do is find out from Sky Broadband if the SMTP server requires authentication or not and what port the SMTP server uses.

  • Getting message cannot send mail check server setting

    Trying to get email to send out, but getting a message saying cannot send...check outgoing server setting...
    Anybody???
    Penni

    Do you have any other mail accounts on your iPad? If so you can select another server from the options.
    Go to settings again and select Mail, Contacts, Calendars. Then select your mail account from the window on the right side. Then tap on the email address of the account to open the next window that shows the account information. Then there should be heading titled Outgoing Mail Server. You can tap the arrow to the right of the server name in that field to get to the next window that shows other servers. Try to select one from there and see if that works.
    I keep forgetting about this as well. It has provided help to others.
    http://mail.google.com/support/bin/answer.py?answer=77702
    Message was edited by: Demo

  • Why can't I send a code to reset my security questions but I have a rescue email address?

    Why can't I send a code to reset my security questions but I have a rescue email address?

    If you have a rescue email address on your account then you will see a reset link on your account : http://support.apple.com/kb/HT6170
    If you only have alternate/secondary email addresses on your account then you won't get the reset link, an you won't be able to add a rescue email address until you can answer your questions - you will have to contact Support in your country to get the questions reset.
    Contacting Apple about account security : http://support.apple.com/kb/HT5699
    If your country isn't on that page then try this form and explain and see what they reply with : https://ssl.apple.com/emea/support/itunes/contact.html
    When they've been reset you can then add one for potential future use : http://support.apple.com/kb/HT5620

  • I am set up mac mini and when I test the mail server by sending a mail from administrator to administrator I get the error message 'Diagnostic-Code: X-Postfix; connect to 127.0.0.1[127.0.0.1]:10024: Connection    refused'. Can any one suggest a solution

    I set up mac mini and when I test the mail server by sending a mail from administrator to administrator I get the error message 'Diagnostic-Code: X-Postfix; connect to 127.0.0.1[127.0.0.1]:10024: Connection    refused'. Can any one suggest a solution

    Thanks. Where could I possibly look for a firewall rule. I knowingly did not creat any. My system has internet coming to a Modem which then is connected to a wireless router. I have computers connecting to the router through cable as well as wireless. One of these computers has been our  mailserver using Mercury and Pegasus and that computer has a static IP address. The Mac Mini also has been assigned a different srtatic IP address. I have only set up user account, none of them an email account as I initially expect them to receive only internal mails. Is it necessary that I must set up an email id for each user while setting up the user account. My server id is server.local. So would the email id for local users be [email protected]?

  • Why can i not get my email to send from my mac? the outgoing server keeps saying its offline?!

    why can i not get my email to send from my mac? the outgoing server keeps saying its offline?! any help would be greatly appreciated

    markwmsn wrote:
    To each his own. I own Outlook, but I avoid using it like the plague. The only Mac mail software I have liked better than Apple's Mail was Qualcomm's Eudora, which is no longer updated or even available, won't run without Rosetta, and unfortunately never quite got the hang of IMAP.
    Check out this post regarding this user's continuing use of Eudora on his Lion MacBook:
    Shield Eudora from Lion's Claws
    A thousand thanks and kudos to Michael for this fabulously excellent thread!
    Eudora's now happy in her new Snow Leopard VM home on my Lion ModBook!
    Appears essential to keep resource-forked data off Lion disk:
    First attempt with Eudora on SL VM was sharing data on Lion disk. Error messages on open like "can't find Table of Contents" suggest Lion ate resource forks crucial to Eudora.
    Found success by transferring data from SL Mac to thumb drive, then mounted drive in SL VM so Lion couldn't touch.
    While Michael warns against storing data in VM, appears there's no choice with Eudora. At least She's enthroned on a mirrored RAID.
    There is Nothing like Eudora! (to tune of "There is Nothing Like a Dame" from South Pacific -- Oops! my gray hair is showing!)
    (It's amazing Parallels can force Lion to store taboo resource forks. Thank God for code warriors!)
    B
    http://forums.macrumors.com/showpost.php?p=16496788&postcount=355

  • I lost the email with my verification code on it how do I get a new verification code?

    I would like to share some of my photos via email, but when I want to share them it asks for a verification code.  I don't have the email with that verification code, how do I get that code or get a new one?

    First go to the Preferences by going to:
    Edit >> Preferences
    And look for something like this:
    Fill in your details and click OK
    Now go to the Share Icon  and follow all the steps as if you are sending a photomail or email attachment and you will get a window where you can request a new code to be resent to you.  See this picture:

  • Not Getting Emails, but can send

    I am not getting emails on my BB.  Last email received on my primary account was Thursday night.  I can send, but nothing is coming through.  It is an account through ATT.com, and it is also the main account I use on my personal laptop through Outlook.  I have made sure that outlook is shut down and laptop is off, but still not receiving. Any thoughts are greatly appreciated!  Thanks!
    Solved!
    Go to Solution.

    Yes, you will need to do so... good luck.
    1. If any post helps you please click the below the post(s) that helped you.
    2. Please resolve your thread by marking the post "Solution?" which solved it for you!
    3. Install free BlackBerry Protect today for backups of contacts and data.
    4. Guide to Unlocking your BlackBerry & Unlock Codes
    Join our BBM Channels (Beta)
    BlackBerry Support Forums Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • I keep getting the message can,t connect to server, can't get email, can't send email from outlook account on ipad

    I keep getting message cannot connect to server, cannot send email, cannot get email from my outlook account . This has been happening for past 3 days Feb 2014

    Check your internet connection...
    If you are connected but can't access anything, your WiFi must be slow.

  • User is getting email delivery failure when processing via SAP as it tries to send an email to a user who have left the company

    Hi Experts,
    I need to remove a name on a group list in Outlook as the user is getting email delivery failure when processing via SAP (He sends the invoice for approval and emails get sent to different users for approval.) One of those users has left the company and so the user gets a email delivery failure error.
    How do I remove this user who is no longer with the company?
    The user is saying: When I approve an invoice in SAP it sends an email notification through outlook. One of those users is no longer with the company so it cannot deliver the notification, and in return send a delivery failure (in outlook). I am approving the invoices from my SAP Workplace inbox.
    How do we know what group the email is going to?
    Please let me know if you have seen this issue before. Greatly appreciate your help.
    Thanks,
    Asad

    Hi,
    Please check whether you are using a custom z program for sending emails of approved invoices to users. If yes then either the user email ids are hardcoded in the program else a ztable is maintained for them.

  • Why can't I send or get email from an AOL account!

    Good Evening,
     I should say if u could try to keep this simple as I am by no means a computer wiz!. Having said that I have a Verizon account & friend's of mine have AOL but for some reason we can not send email to each other as it keeps coming back.
     My question is why is this? I love Verizon but have never had this with any of the other providers I had. Is there something I need to turn off? & if so what & how?. I'm really lost as to why I can't send nor get email from AOL. If anyone could explain it but in a way I can understand it this would be great!.
     Thank you for your time!
       Will, 

    my70chevelless wrote:
    Good Evening,
     I should say if u could try to keep this simple as I am by no means a computer wiz!. Having said that I have a Verizon account & friend's of mine have AOL but for some reason we can not send email to each other as it keeps coming back.
     My question is why is this? I love Verizon but have never had this with any of the other providers I had. Is there something I need to turn off? & if so what & how?. I'm really lost as to why I can't send nor get email from AOL. If anyone could explain it but in a way I can understand it this would be great!.
     Thank you for your time!
       Will, 
    What message are you getting when it comes back? Having that info would help isolate the problem.
    If a forum member gives an answer you like, give them the Kudos they deserve. If a member gives you the answer to your question, mark the answer as Accepted Solution so others can see the solution to the problem.
    "All knowledge is worth having."

Maybe you are looking for