Example usage for javax.mail.internet InternetAddress getAddress

List of usage examples for javax.mail.internet InternetAddress getAddress

Introduction

In this page you can find the example usage for javax.mail.internet InternetAddress getAddress.

Prototype

public String getAddress() 

Source Link

Document

Get the email address.

Usage

From source file:org.beangle.notification.notifiers.mail.MimeUtils.java

public static final List<InternetAddress> parseAddress(String address, String encoding) {
    if (StringUtils.isEmpty(address))
        return Collections.emptyList();
    try {/*w w  w .  j  a  v  a2 s .  com*/
        InternetAddress[] parsed = InternetAddress.parse(address);
        List<InternetAddress> returned = CollectUtils.newArrayList();
        for (InternetAddress raw : parsed) {
            returned.add((encoding != null ? new InternetAddress(raw.getAddress(), raw.getPersonal(), encoding)
                    : raw));
        }
        return returned;
    } catch (Exception ex) {
        throw new RuntimeException("Failed to parse embedded personal name to correct encoding", ex);
    }
}

From source file:Main.java

public static String getAddressString(Address[] addresses) {
    if (addresses == null || addresses.length <= 0) {
        return "";
    }/* w  w w  .  j ava 2s.co m*/
    HashSet<String> foundAddress = new HashSet<String>();

    StringBuffer sb = new StringBuffer();
    InternetAddress addr = null;
    int length = addresses.length;
    for (int i = 0; i < length; i++) {
        addr = (InternetAddress) addresses[i];
        if (foundAddress.contains(addr.getAddress())) {
            continue;
        }
        foundAddress.add(addr.getAddress());

        if (addr.getPersonal() != null) {
            String personaladdr = getOneString(getTokens(addr.getPersonal(), "\r\n"), "");
            sb.append(personaladdr).append(" <").append(addr.getAddress()).append(">, ");
        } else {
            sb.append(addr.getAddress()).append(", ");
        }
    }
    String addressStr = sb.toString();
    return addressStr.substring(0, addressStr.length() - 2);
}

From source file:org.springframework.ws.transport.mail.support.MailTransportUtils.java

/**
 * Converts the given internet address into a <code>mailto</code> URI.
 *
 * @param to      the To: address//w w  w .  ja va  2s. c  o  m
 * @param subject the subject, may be <code>null</code>
 * @return a mailto URI
 */
public static URI toUri(InternetAddress to, String subject) throws URISyntaxException {
    if (StringUtils.hasLength(subject)) {
        return new URI(MailTransportConstants.MAIL_URI_SCHEME, to.getAddress() + "?subject=" + subject, null);
    } else {
        return new URI(MailTransportConstants.MAIL_URI_SCHEME, to.getAddress(), null);
    }
}

From source file:com.liferay.petra.mail.InternetAddressUtil.java

public static String toString(Address address) {
    InternetAddress internetAddress = (InternetAddress) address;

    if (internetAddress != null) {
        StringBundler sb = new StringBundler(5);

        String personal = internetAddress.getPersonal();
        String emailAddress = internetAddress.getAddress();

        if (Validator.isNotNull(personal)) {
            sb.append(personal);/*from w w w  .  ja  va2  s  . co  m*/
            sb.append(StringPool.SPACE);
            sb.append(StringPool.LESS_THAN);
            sb.append(emailAddress);
            sb.append(StringPool.GREATER_THAN);
        } else {
            sb.append(emailAddress);
        }

        return sb.toString();
    }

    return StringPool.BLANK;
}

From source file:de.bht.fpa.mail.s000000.common.mail.imapsync.MessageConverter.java

private static void convertSender(javax.mail.Message javaMailMessage, MessageBuilder messageBuilder)
        throws MessagingException {
    Address[] from = javaMailMessage.getFrom();
    if (from == null || from.length <= 0) {
        return;/*from ww w.ja  v  a2  s  .c o  m*/
    }

    Address firstFrom = from[0];
    if (!(firstFrom instanceof InternetAddress)) {
        return;
    }
    InternetAddress internetAddress = (InternetAddress) firstFrom;
    messageBuilder.sender(
            newSenderBuilder().email(internetAddress.getAddress()).personal(internetAddress.getPersonal()));
}

From source file:de.bht.fpa.mail.s000000.common.mail.imapsync.MessageConverter.java

private static void addRecipients(MessageBuilder messageBuilder, RecipientType recipientType,
        Address[] recipients) {/*from   ww  w  .  ja  va  2s  .com*/
    if (recipients == null) {
        return;
    }
    for (Address address : recipients) {
        if (!(address instanceof InternetAddress)) {
            continue;
        }
        InternetAddress internetAddress = (InternetAddress) address;

        // @formatter:off
        messageBuilder.recipient(newRecipientBuilder().email(internetAddress.getAddress())
                .personal(internetAddress.getPersonal()).type(recipientType));
        // @formatter:on
    }
}

From source file:mitm.common.mail.EmailAddressUtils.java

public static boolean isValid(InternetAddress address) {
    return address != null && validate(address.getAddress()) != null;
}

From source file:com.silverpeas.importExport.control.PublicationImportExport.java

/**
 * Mthodes permettant de rcuprer un objet publication dont les mta-donnes sont gnres 
 * partir des informations du fichier destin  tre attach  celle ci. Utilisation de l'api POI
 * dans le cas des fichiers MSoffice./*from   w ww . j a  v a 2s . c  o  m*/
 * @param userDetail - contient les informations sur l'utilisateur du moteur d'importExport
 * @param file - fichier destin  tre attach  la publication d'o l'on extrait les
 * informations qui iront renseigner les mta-donnes de la publication  creer
 * @param isPOIUsed
 * @return renvoie un objet PublicationDetail
 */
public static PublicationDetail convertFileInfoToPublicationDetail(File file, ImportSettings settings) {
    String fileName = file.getName();
    String nomPub = settings.getPublicationName(fileName);
    String description = "";
    String motsClefs = "";
    String content = "";
    Date creationDate = new Date();
    Date lastModificationDate = null;

    if (FileUtil.isMail(file.getName())) {
        try {
            MailExtractor extractor = Extractor.getExtractor(file);
            Mail mail = extractor.getMail();

            creationDate = mail.getDate();

            // define StringTemplate attributes
            Map<String, String> attributes = new HashMap<String, String>();
            attributes.put("subject", mail.getSubject());
            InternetAddress address = mail.getFrom();
            if (StringUtil.isDefined(address.getPersonal())) {
                attributes.put("fromPersonal", address.getPersonal());
                description += address.getPersonal() + " - ";
            }
            attributes.put("fromAddress", address.getAddress());

            // generate title of publication
            StringTemplate titleST = new StringTemplate(multilang.getString("importExport.import.mail.title"));
            titleST.setAttributes(attributes);
            nomPub = titleST.toString();

            // generate description of publication
            StringTemplate descriptionST = new StringTemplate(
                    multilang.getString("importExport.import.mail.description"));
            descriptionST.setAttributes(attributes);
            description = descriptionST.toString();
        } catch (Exception e) {
            SilverTrace.error("importExport", "PublicationImportExport.convertFileInfoToPublicationDetail",
                    "importExport.EX_CANT_EXTRACT_MAIL_DATA", e);
        }
    } else {
        // it's a classical file (not an email)
        MetaData metaData = null;
        if (settings.isPoiUsed()) {
            // extract title, subject and keywords
            metaData = metadataExtractor.extractMetadata(file.getAbsolutePath());
            if (StringUtil.isDefined(metaData.getTitle())) {
                nomPub = metaData.getTitle();
            }
            if (StringUtil.isDefined(metaData.getSubject())) {
                description = metaData.getSubject();
            }
            if (metaData.getKeywords() != null && metaData.getKeywords().length > 0) {
                motsClefs = StringUtils.join(metaData.getKeywords(), ';');
            }
        }

        if (settings.useFileDates()) {
            // extract creation and last modification dates
            if (metaData == null) {
                metaData = metadataExtractor.extractMetadata(file.getAbsolutePath());
            }
            if (metaData.getCreationDate() != null) {
                creationDate = metaData.getCreationDate();
            }
            if (metaData.getLastSaveDateTime() != null) {
                lastModificationDate = metaData.getLastSaveDateTime();
            }
        }
    }
    PublicationDetail publication = new PublicationDetail("unknown", nomPub, description, creationDate,
            new Date(), null, settings.getUser().getId(), "5", null, motsClefs, content);
    if (lastModificationDate != null) {
        publication.setUpdateDate(lastModificationDate);
        publication.setUpdateDateMustBeSet(true);
    }
    return publication;
}

From source file:com.glaf.mail.util.MailUtils.java

public static String getMailAddress(Address aaddress[]) {
    String mailAddress = "";
    if (aaddress != null) {
        try {/*from www .jav a  2s .  co  m*/
            StringBuffer addrBuffer = new StringBuffer();
            for (int j = 0; j < aaddress.length; j++) {
                InternetAddress internetaddress = (InternetAddress) aaddress[j];
                if (internetaddress.getPersonal() == null || internetaddress.getPersonal().length() == 0) {
                    addrBuffer.append(internetaddress.getAddress()).append(" < ")
                            .append(internetaddress.getAddress()).append(" > ");
                } else {
                    addrBuffer.append(internetaddress.getPersonal()).append(" < ")
                            .append(internetaddress.getAddress()).append(" > ");
                }
            }
            String addr = addrBuffer.toString();
            if (addr.startsWith("=?GBK?Q?=") || addr.startsWith("=?GB2312?Q?=")) {
                addr = convertString(addr);
            }
            mailAddress = addr;
        } catch (Exception ex) {
        }
    }

    return mailAddress;
}

From source file:mitm.common.mail.EmailAddressUtils.java

/**
 * Checks whether the given email address is a valid email address using strict checks
 * @param email// ww  w  .j  a va2  s .  c o  m
 * @return email address if email is valid, null if email is invalid
 */
public static String validate(String email) {
    /* email size must be smaller than max domain + max local */
    if (email != null && email.length() < (255 + 65)) {
        try {
            InternetAddress validated = new InternetAddress(email, true /* strict checking */);

            return validated.getAddress();
        } catch (AddressException ignored) {
            logger.debug("Email address \"{}\" is not a valid email address", email);
        }
    }
    return null;
}