Java Code Examples for javax.mail.internet.InternetAddress#getPersonal()

The following examples show how to use javax.mail.internet.InternetAddress#getPersonal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavamailService.java    From lemon with Apache License 2.0 6 votes vote down vote up
public static String getFrom(MimeMessage msg) throws MessagingException,
        UnsupportedEncodingException {
    String from = "";
    Address[] froms = msg.getFrom();

    if (froms.length < 1) {
        throw new MessagingException("没有发件人!");
    }

    InternetAddress address = (InternetAddress) froms[0];
    String person = address.getPersonal();

    if (person != null) {
        person = MimeUtility.decodeText(person) + " ";
    } else {
        person = "";
    }

    from = person + "<" + address.getAddress() + ">";

    return from;
}
 
Example 2
Source File: MailUtils.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 用来取得 Email 地址的友好称呼。
 * @param emailAddress
 *            	Email 地址
 * @return String
 * 				例如: emailAddress="John &lt;[email protected]&gt;" 就返回 John。
 */

public static String getAddressName(String emailAddress) {
	String result = "";
	if (emailAddress == null) {
		return result;
	}
	try {
		InternetAddress address = new InternetAddress(emailAddress);
		String text = address.getPersonal();
		if (text == null) {
			result = emailAddress;
		} else {
			result = text;
		}
	} catch (AddressException e) {
		e.printStackTrace();
		return emailAddress;
	}
	return result;
}
 
Example 3
Source File: Pop3Util.java    From anyline with Apache License 2.0 6 votes vote down vote up
/**  
   * 获得邮件发件人  
   * @param msg 邮件内容  
   * @return 姓名 &lt;Email地址&gt;  
   */   
  public static String getFrom(MimeMessage msg){   
      String from = "";   
      Address[] froms; 
try { 
	froms = msg.getFrom(); 
       InternetAddress address = (InternetAddress) froms[0];   
       String person = address.getPersonal();   
       if (person != null) {   
           person = MimeUtility.decodeText(person) + " ";   
       } else {   
           person = "";   
       }   
       from = person + "<" + address.getAddress() + ">";   
} catch (Exception e) { 
	e.printStackTrace(); 
}   
      return from;   
  }
 
Example 4
Source File: MailAddress.java    From greenmail with Apache License 2.0 6 votes vote down vote up
public MailAddress(String str)
        throws AddressException {

    // Decoding the mail address in
    // case it contains non us-ascii characters
    String decoded = decodeStr(str);

    InternetAddress address = new InternetAddress();
    address.setAddress(decoded);
    email = address.getAddress();
    name = address.getPersonal();

    String[] strs = email.split("@");
    user = strs[0];
    if (strs.length > 1) {
        host = strs[1];
    } else {
        host = "localhost";
    }
}
 
Example 5
Source File: MailUtils.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 用来取得 Email 地址的友好称呼。
 * @param emailAddress
 *            	Email 地址
 * @return String
 * 				例如: emailAddress="John &lt;[email protected]&gt;" 就返回 John。
 */

public static String getAddressName(String emailAddress) {
	String result = "";
	if (emailAddress == null) {
		return result;
	}
	try {
		InternetAddress address = new InternetAddress(emailAddress);
		String text = address.getPersonal();
		if (text == null) {
			result = emailAddress;
		} else {
			result = text;
		}
	} catch (AddressException e) {
		e.printStackTrace();
		return emailAddress;
	}
	return result;
}
 
Example 6
Source File: MailUtils.java    From scada with MIT License 6 votes vote down vote up
/**
 * ����ʼ�������
 * @param msg �ʼ�����
 * @return ���� <Email��ַ>
 */ 
public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException { 
    String from = ""; 
    Address[] froms = msg.getFrom(); 
    if (froms.length < 1){
    	//return "ϵͳ�ָ�";
        throw new MessagingException("û�з�����!");
    }         
    InternetAddress address = (InternetAddress) froms[0]; 
    String person = address.getPersonal(); 
    if (person != null) { 
        person = MimeUtility.decodeText(person) + " "; 
    } else { 
        person = ""; 
    } 
    from = person + "<" + address.getAddress() + ">"; 
     
    return from; 
}
 
Example 7
Source File: ContactInfo.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
static Address[] fillIn(Address[] addresses, boolean prefer_contact) {
    if (addresses == null)
        return null;

    Address[] modified = new Address[addresses.length];
    for (int i = 0; i < addresses.length; i++) {
        InternetAddress address = (InternetAddress) addresses[i];
        String email = address.getAddress();
        String personal = address.getPersonal();
        if (!TextUtils.isEmpty(email)) {
            Lookup lookup = emailLookup.get(email.toLowerCase(Locale.ROOT));
            if (lookup != null &&
                    (TextUtils.isEmpty(personal) || prefer_contact))
                personal = lookup.displayName;
        }
        try {
            modified[i] = new InternetAddress(email, personal, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException ex) {
            Log.e(ex);
            modified[i] = address;
        }
    }

    return modified;
}
 
Example 8
Source File: MailUtil.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Extracts the address name
 * 
 * @param a the address
 * 
 * @return the name
 */
public static String getAddressName(Address a) {
	if (a != null) {
		InternetAddress ia = (InternetAddress) a;

		if (ia.getPersonal() != null) {
			return ia.getPersonal();
		} else {
			return ia.getAddress();
		}
	} else {
		return "";
	}
}
 
Example 9
Source File: MimeMessageHelper.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private InternetAddress parseAddress(String address) throws MessagingException {
	InternetAddress[] parsed = InternetAddress.parse(address);
	if (parsed.length != 1) {
		throw new AddressException("Illegal address", address);
	}
	InternetAddress raw = parsed[0];
	try {
		return (getEncoding() != null ?
				new InternetAddress(raw.getAddress(), raw.getPersonal(), getEncoding()) : raw);
	}
	catch (UnsupportedEncodingException ex) {
		throw new MessagingException("Failed to parse embedded personal name to correct encoding", ex);
	}
}
 
Example 10
Source File: MimeMessageHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private InternetAddress parseAddress(String address) throws MessagingException {
	InternetAddress[] parsed = InternetAddress.parse(address);
	if (parsed.length != 1) {
		throw new AddressException("Illegal address", address);
	}
	InternetAddress raw = parsed[0];
	try {
		return (getEncoding() != null ?
				new InternetAddress(raw.getAddress(), raw.getPersonal(), getEncoding()) : raw);
	}
	catch (UnsupportedEncodingException ex) {
		throw new MessagingException("Failed to parse embedded personal name to correct encoding", ex);
	}
}
 
Example 11
Source File: ImapConnection.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
protected void appendMailEnvelopeHeader(StringBuilder buffer, String[] value) {
    buffer.append(' ');
    if (value != null && value.length > 0) {
        try {
            String unfoldedValue = MimeUtility.unfold(value[0]);
            InternetAddress[] addresses = InternetAddress.parseHeader(unfoldedValue, false);
            if (addresses.length > 0) {
                buffer.append('(');
                for (InternetAddress address : addresses) {
                    buffer.append('(');
                    String personal = address.getPersonal();
                    if (personal != null) {
                        appendEnvelopeHeaderValue(buffer, personal);
                    } else {
                        buffer.append("NIL");
                    }
                    buffer.append(" NIL ");
                    String mail = address.getAddress();
                    int atIndex = mail.indexOf('@');
                    if (atIndex >= 0) {
                        buffer.append('"').append(mail, 0, atIndex).append('"');
                        buffer.append(' ');
                        buffer.append('"').append(mail.substring(atIndex + 1)).append('"');
                    } else {
                        buffer.append("NIL NIL");
                    }
                    buffer.append(')');
                }
                buffer.append(')');
            } else {
                buffer.append("NIL");
            }
        } catch (AddressException | UnsupportedEncodingException e) {
            DavGatewayTray.warn(e);
            buffer.append("NIL");
        }
    } else {
        buffer.append("NIL");
    }
}
 
Example 12
Source File: MimeMessageHelper.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private InternetAddress parseAddress(String address) throws MessagingException {
	InternetAddress[] parsed = InternetAddress.parse(address);
	if (parsed.length != 1) {
		throw new AddressException("Illegal address", address);
	}
	InternetAddress raw = parsed[0];
	try {
		return (getEncoding() != null ?
				new InternetAddress(raw.getAddress(), raw.getPersonal(), getEncoding()) : raw);
	}
	catch (UnsupportedEncodingException ex) {
		throw new MessagingException("Failed to parse embedded personal name to correct encoding", ex);
	}
}
 
Example 13
Source File: MessageHelper.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private Address[] getAddressHeader(String name) throws MessagingException {
    ensureMessage(false);

    String header = imessage.getHeader(name, ",");
    if (header == null)
        return null;

    header = fixEncoding(name, header);
    header = header.replaceAll("\\?=[\\r\\n\\t ]+=\\?", "\\?==\\?");
    Address[] addresses = InternetAddress.parseHeader(header, false);

    for (Address address : addresses) {
        InternetAddress iaddress = (InternetAddress) address;
        String email = iaddress.getAddress();
        String personal = iaddress.getPersonal();

        email = decodeMime(email);
        if (!Helper.isSingleScript(email))
            email = punyCode(email);

        iaddress.setAddress(email);

        if (personal != null) {
            try {
                iaddress.setPersonal(decodeMime(personal));
            } catch (UnsupportedEncodingException ex) {
                Log.w(ex);
            }
        }
    }

    return addresses;
}
 
Example 14
Source File: Shortcuts.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
static ShortcutInfoCompat.Builder getShortcut(Context context, InternetAddress address) {
    String name = address.getPersonal();
    String email = address.getAddress();

    Uri lookupUri = null;
    boolean contacts = Helper.hasPermission(context, Manifest.permission.READ_CONTACTS);
    if (contacts) {
        ContentResolver resolver = context.getContentResolver();
        try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                new String[]{
                        ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
                        ContactsContract.Contacts.LOOKUP_KEY,
                        ContactsContract.Contacts.DISPLAY_NAME
                },
                ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
                new String[]{email}, null)) {
            if (cursor != null && cursor.moveToNext()) {
                int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
                int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
                int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

                long contactId = cursor.getLong(colContactId);
                String lookupKey = cursor.getString(colLookupKey);
                String displayName = cursor.getString(colDisplayName);

                lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
                if (!TextUtils.isEmpty(displayName))
                    name = displayName;
            }
        }
    }

    return getShortcut(context, email, name, lookupUri);
}
 
Example 15
Source File: MimeMessageHelper.java    From java-technology-stack with MIT License 5 votes vote down vote up
private InternetAddress parseAddress(String address) throws MessagingException {
	InternetAddress[] parsed = InternetAddress.parse(address);
	if (parsed.length != 1) {
		throw new AddressException("Illegal address", address);
	}
	InternetAddress raw = parsed[0];
	try {
		return (getEncoding() != null ?
				new InternetAddress(raw.getAddress(), raw.getPersonal(), getEncoding()) : raw);
	}
	catch (UnsupportedEncodingException ex) {
		throw new MessagingException("Failed to parse embedded personal name to correct encoding", ex);
	}
}
 
Example 16
Source File: MediatypeEmailImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Getter for property replyAdr.
 * 
 * @return Value of property replyAdr.
 */
@Override
public String getReplyAdr() throws Exception {
	InternetAddress tmpReply = new InternetAddress(replyEmail, replyFullname, charset);
	if (StringUtils.isNotBlank(tmpReply.getPersonal())) {
		return tmpReply.getPersonal() + " <" + tmpReply.getAddress() + ">";
	} else {
		return tmpReply.getAddress();
	}
}
 
Example 17
Source File: MediatypeEmailImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public String getFromAdr() throws Exception {
	InternetAddress tmpFrom = new InternetAddress(fromEmail, fromFullname, charset);
	if (StringUtils.isNotBlank(tmpFrom.getPersonal())) {
		return tmpFrom.getPersonal() + " <" + tmpFrom.getAddress() + ">";
	} else {
		return tmpFrom.getAddress();
	}
}
 
Example 18
Source File: MassMailRecipientDaoImpl.java    From ctsms with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static EmailAddressVO internetAddressToEmailAddressVO(InternetAddress address) {
	return new EmailAddressVO(address.getAddress(), address.getPersonal());
}
 
Example 19
Source File: MessageHelper.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static String formatAddresses(Address[] addresses, boolean full, boolean compose) {
    if (addresses == null || addresses.length == 0)
        return "";

    List<String> formatted = new ArrayList<>();
    for (int i = 0; i < addresses.length; i++) {
        boolean duplicate = false;
        for (int j = 0; j < i; j++)
            if (addresses[i].equals(addresses[j])) {
                duplicate = true;
                break;
            }
        if (duplicate)
            continue;

        if (addresses[i] instanceof InternetAddress) {
            InternetAddress address = (InternetAddress) addresses[i];
            String email = address.getAddress();
            String personal = address.getPersonal();

            if (TextUtils.isEmpty(personal))
                formatted.add(email);
            else {
                if (compose) {
                    boolean quote = false;
                    personal = personal.replace("\"", "");
                    for (int c = 0; c < personal.length(); c++)
                        // https://tools.ietf.org/html/rfc822
                        if ("()<>@,;:\\\".[]".indexOf(personal.charAt(c)) >= 0) {
                            quote = true;
                            break;
                        }
                    if (quote)
                        personal = "\"" + personal + "\"";
                }

                if (full)
                    formatted.add(personal + " <" + email + ">");
                else
                    formatted.add(personal);
            }
        } else
            formatted.add(addresses[i].toString());
    }
    return TextUtils.join(", ", formatted);
}