Example usage for javax.mail.internet InternetAddress getPersonal

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

Introduction

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

Prototype

public String getPersonal() 

Source Link

Document

Get the personal name.

Usage

From source file:com.googlecode.gmail4j.javamail.JavaMailGmailMessage.java

@Override
public EmailAddress getFrom() {
    if (from == null) {
        try {//  ww w .j  a  v a2s  . co  m
            final InternetAddress f = (InternetAddress) source.getFrom()[0];
            from = new EmailAddress(f.getPersonal(), f.getAddress());
        } catch (final Exception e) {
            throw new GmailException("Failed getting from address", e);
        }
    }
    return from;
}

From source file:com.duroty.application.open.manager.OpenManager.java

/**
 * DOCUMENT ME!//ww w.  j av  a  2  s. c o  m
 *
 * @param session DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param from DOCUMENT ME!
 * @param to DOCUMENT ME!
 * @param subject DOCUMENT ME!
 * @param body DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
private void sendData(Session msession, InternetAddress from, InternetAddress to, String username,
        String password, String signature) throws Exception {
    try {
        HtmlEmail email = new HtmlEmail();
        email.setMailSession(msession);

        email.setFrom(from.getAddress(), from.getPersonal());
        email.addTo(to.getAddress(), to.getPersonal());

        email.setSubject("Duroty System");
        email.setHtmlMsg("<p>Username: <b>" + username + "</b></p><p>Password: " + password + "<b></b></p><p>"
                + signature + "</p>");

        email.setCharset(MimeUtility.javaCharset(Charset.defaultCharset().displayName()));

        email.send();
    } finally {
    }
}

From source file:org.agnitas.beans.impl.MediatypeEmailImpl.java

public void setParam(String param) throws Exception {
    int tmp = 0;//w ww  .j a  va  2 s . c o m
    String from = unEscape(AgnUtils.findParam("from", param));

    if (from.length() > 0) {
        InternetAddress adr = new InternetAddress(from);

        this.fromEmail = adr.getAddress();
        this.fromFullname = adr.getPersonal();
    } else {
        this.fromEmail = "";
        this.fromFullname = "";
    }

    from = AgnUtils.findParam("reply", param);
    if (from == null) {
        from = AgnUtils.findParam("from", param);
    }

    if (from.length() > 0) {
        InternetAddress adr = new InternetAddress(from);

        this.replyEmail = adr.getAddress();
        this.replyFullname = adr.getPersonal();
    } else {
        this.replyEmail = "";
        this.replyFullname = "";
    }

    this.charset = AgnUtils.findParam("charset", param);
    if (this.charset == null) {
        this.charset = DEFAULT_CHARSET;
    }
    this.subject = AgnUtils.findParam("subject", param);
    try {
        tmp = Integer.parseInt(AgnUtils.findParam("mailformat", param));
    } catch (Exception e) {
        tmp = 2; // default: Offline-HTML
    }
    this.mailFormat = tmp;
    try {
        tmp = Integer.parseInt(AgnUtils.findParam("linefeed", param));
    } catch (Exception e) {
        tmp = 72; // default: after 72 characters
    }
    this.linefeed = tmp;

    this.onepixel = AgnUtils.findParam("onepixlog", param);
    if (this.onepixel == null) {
        this.onepixel = MediatypeEmailImpl.ONEPIXEL_NONE;
    }
}

From source file:edu.stanford.muse.email.EmailFetcherStats.java

/**
 * intern a bunch of addrs, to save memory
 *
 * @throws UnsupportedEncodingException/*from   www.  j  a  va2  s. c o m*/
 */
private static void internAddressList(Address[] addrs) throws UnsupportedEncodingException {
    if (addrs == null)
        return;

    for (Address a : addrs) {
        if (a instanceof InternetAddress) {
            InternetAddress ia = (InternetAddress) a;
            String address = ia.getAddress(), personal = ia.getPersonal();
            if (address != null)
                ia.setAddress(InternTable.intern(address));
            if (personal != null)
                ia.setPersonal(InternTable.intern(personal));
        }
    }
}

From source file:org.agnitas.beans.impl.MediatypeEmailImpl.java

public String getFromAdr() throws Exception {
    InternetAddress tmpFrom = new InternetAddress(this.fromEmail, this.fromFullname, charset);
    //problems with coding
    //return AgnUtils.propertySaveString(tmpFrom.toString());

    String adr = "";
    adr = tmpFrom.getPersonal() + " <" + tmpFrom.getAddress() + ">";
    return adr;//from  w  ww .j  a  v  a 2 s  .co m
}

From source file:org.silverpeas.core.mail.TestSmtpMailSending.java

private void assertMailSent(MailToSend verifiedMailToSend) throws Exception {
    assertThat("assertMailSent is compatible with one receiver only...", verifiedMailToSend.getTo(),
            hasSize(1));// w w w  .ja va2 s. c  om

    assertThat(verifiedMailToSend.getTo().getRecipientType().getTechnicalType(), is(Message.RecipientType.TO));

    MimeMessage[] messages = greenMailRule.getReceivedMessages();
    assertThat(messages, arrayWithSize(1));

    MimeMessage sentMessage = messages[0];
    MailAddress originalReceiverMailAddress = verifiedMailToSend.getTo().iterator().next();

    assertThat(sentMessage.getFrom().length, is(1));
    assertThat(sentMessage.getFrom()[0], instanceOf(InternetAddress.class));
    InternetAddress internetAddressFrom = (InternetAddress) sentMessage.getFrom()[0];
    assertThat(internetAddressFrom.getAddress(), is(verifiedMailToSend.getFrom().getEmail()));
    assertThat(internetAddressFrom.getPersonal(),
            is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null)));

    assertThat(sentMessage.getRecipients(Message.RecipientType.TO).length, is(1));
    assertThat(sentMessage.getRecipients(Message.RecipientType.CC), nullValue());
    assertThat(sentMessage.getRecipients(Message.RecipientType.BCC), nullValue());
    assertThat(sentMessage.getRecipients(Message.RecipientType.TO)[0], instanceOf(InternetAddress.class));
    InternetAddress internetAddressTo = (InternetAddress) sentMessage
            .getRecipients(Message.RecipientType.TO)[0];
    assertThat(internetAddressTo.getAddress(), is(originalReceiverMailAddress.getEmail()));
    assertThat(internetAddressTo.getPersonal(), nullValue());

    assertThat(sentMessage.getSubject(), is(verifiedMailToSend.getSubject()));
    if (verifiedMailToSend.getContent().getValue() instanceof Multipart) {
        assertThat(sentMessage.getContent(), instanceOf(verifiedMailToSend.getContent().getValue().getClass()));
    } else {
        assertThat(sentMessage.getContent().toString().replaceAll("[\n\r]*$", ""),
                is(verifiedMailToSend.getContent().getValue()));
    }

    assertThat(DateUtils.addSeconds(sentMessage.getSentDate(), 10), greaterThanOrEqualTo(new Date()));

    assertThat(sentMessage.getReplyTo().length, is(1));
    if (verifiedMailToSend.isReplyToRequired()) {
        assertThat(sentMessage.getHeader("Reply-To"), notNullValue());
        assertThat(sentMessage.getReplyTo()[0], instanceOf(InternetAddress.class));
        InternetAddress internetAddressReplyTo = (InternetAddress) sentMessage.getReplyTo()[0];
        assertThat(internetAddressReplyTo.getAddress(), is(verifiedMailToSend.getFrom().getEmail()));
        assertThat(internetAddressReplyTo.getPersonal(),
                is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null)));
    } else {
        assertThat(sentMessage.getHeader("Reply-To"), nullValue());
        assertThat(sentMessage.getReplyTo()[0].toString(), is(internetAddressFrom.toString()));
    }
}

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

private void processMailContent(PublicationDetail pubDetail, File file, ImportReportManager reportManager,
        UnitReport unitReport, GEDImportExport gedIE, boolean isVersioningUsed) throws ImportExportException {

    String componentId = gedIE.getCurrentComponentId();
    UserDetail userDetail = gedIE.getCurentUserDetail();
    MailExtractor extractor = null;//from  ww w  .  ja v  a2  s  .  com
    Mail mail = null;
    try {
        extractor = Extractor.getExtractor(file);
        mail = extractor.getMail();
    } catch (Exception e) {
        SilverTrace.error("importExport", "RepositoriesTypeManager.processMailContent",
                "importExport.EX_CANT_EXTRACT_MAIL_DATA", e);
    }
    if (mail != null) {
        // save mail data into dedicated form
        String content = mail.getBody();
        PublicationContentType pubContent = new PublicationContentType();
        XMLModelContentType modelContent = new XMLModelContentType("mail");
        pubContent.setXMLModelContentType(modelContent);
        List<XMLField> fields = new ArrayList<XMLField>();
        modelContent.setFields(fields);

        XMLField subject = new XMLField("subject", mail.getSubject());
        fields.add(subject);

        XMLField body = new XMLField("body", ESCAPE_ISO8859_1.translate(content));
        fields.add(body);

        XMLField date = new XMLField("date", DateUtil.getOutputDateAndHour(mail.getDate(), "fr"));
        fields.add(date);

        InternetAddress address = mail.getFrom();
        String from = "";
        if (StringUtil.isDefined(address.getPersonal())) {
            from += address.getPersonal() + " - ";
        }
        from += "<a href=\"mailto:" + address.getAddress() + "\">" + address.getAddress() + "</a>";
        XMLField fieldFROM = new XMLField("from", from);
        fields.add(fieldFROM);

        Address[] recipients = mail.getAllRecipients();
        String to = "";
        for (Address recipient : recipients) {
            InternetAddress ia = (InternetAddress) recipient;
            if (StringUtil.isDefined(ia.getPersonal())) {
                to += ia.getPersonal() + " - ";
            }
            to += "<a href=\"mailto:" + ia.getAddress() + "\">" + ia.getAddress() + "</a></br>";
        }
        XMLField fieldTO = new XMLField("to", to);
        fields.add(fieldTO);

        // save form
        gedIE.createPublicationContent(reportManager, unitReport, Integer.parseInt(pubDetail.getPK().getId()),
                pubContent, userDetail.getId(), null);

        // extract each file from mail...
        try {
            List<AttachmentDetail> documents = new ArrayList<AttachmentDetail>();

            List<MailAttachment> attachments = extractor.getAttachments();
            for (MailAttachment attachment : attachments) {
                if (attachment != null) {
                    AttachmentDetail attDetail = new AttachmentDetail();
                    AttachmentPK pk = new AttachmentPK("unknown", "useless", componentId);
                    attDetail.setLogicalName(attachment.getName());
                    attDetail.setPhysicalName(attachment.getPath());
                    attDetail.setAuthor(userDetail.getId());
                    attDetail.setSize(attachment.getSize());
                    attDetail.setPK(pk);

                    documents.add(attDetail);
                }
            }

            // ... and save it
            if (isVersioningUsed) {
                // versioning mode
                VersioningImportExport versioningIE = new VersioningImportExport(userDetail);
                versioningIE.importDocuments(pubDetail.getPK().getId(), componentId, documents,
                        Integer.parseInt(userDetail.getId()), pubDetail.isIndexable());
            } else {
                // classic mode
                AttachmentImportExport attachmentIE = new AttachmentImportExport(gedIE.getCurentUserDetail());
                attachmentIE.importAttachments(pubDetail.getPK().getId(), componentId, documents,
                        userDetail.getId(), pubDetail.isIndexable());
            }
        } catch (Exception e) {
            SilverTrace.error("importExport", "RepositoriesTypeManager.processMailContent",
                    "root.EX_NO_MESSAGE", e);
        }
    }
}

From source file:org.silverpeas.core.mail.SmtpMailSendingTest.java

private void assertMailSent(MailToSend verifiedMailToSend, GreenMailOperations mail) throws Exception {
    assertThat("assertMailSent is compatible with one receiver only...", verifiedMailToSend.getTo(),
            hasSize(1));//  ww w  .  j a  va2s .c o  m

    assertThat(verifiedMailToSend.getTo().getRecipientType().getTechnicalType(), is(Message.RecipientType.TO));

    MimeMessage[] messages = mail.getReceivedMessages();
    assertThat(messages, arrayWithSize(1));

    MimeMessage sentMessage = messages[0];
    MailAddress originalReceiverMailAddress = verifiedMailToSend.getTo().iterator().next();

    assertThat(sentMessage.getFrom().length, is(1));
    assertThat(sentMessage.getFrom()[0], instanceOf(InternetAddress.class));
    InternetAddress internetAddressFrom = (InternetAddress) sentMessage.getFrom()[0];
    assertThat(internetAddressFrom.getAddress(), is(verifiedMailToSend.getFrom().getEmail()));
    assertThat(internetAddressFrom.getPersonal(),
            is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null)));

    assertThat(sentMessage.getRecipients(Message.RecipientType.TO).length, is(1));
    assertThat(sentMessage.getRecipients(Message.RecipientType.CC), nullValue());
    assertThat(sentMessage.getRecipients(Message.RecipientType.BCC), nullValue());
    assertThat(sentMessage.getRecipients(Message.RecipientType.TO)[0], instanceOf(InternetAddress.class));
    InternetAddress internetAddressTo = (InternetAddress) sentMessage
            .getRecipients(Message.RecipientType.TO)[0];
    assertThat(internetAddressTo.getAddress(), is(originalReceiverMailAddress.getEmail()));
    assertThat(internetAddressTo.getPersonal(), nullValue());

    assertThat(sentMessage.getSubject(), is(verifiedMailToSend.getSubject()));
    if (verifiedMailToSend.getContent().getValue() instanceof Multipart) {
        assertThat(sentMessage.getContent(), instanceOf(verifiedMailToSend.getContent().getValue().getClass()));
    } else {
        assertThat(sentMessage.getContent().toString().replaceAll("[\n\r]*$", ""),
                is(verifiedMailToSend.getContent().getValue()));
    }

    assertThat(DateUtils.addSeconds(sentMessage.getSentDate(), 10), greaterThanOrEqualTo(new Date()));

    assertThat(sentMessage.getReplyTo().length, is(1));
    if (verifiedMailToSend.isReplyToRequired()) {
        assertThat(sentMessage.getHeader("Reply-To"), notNullValue());
        assertThat(sentMessage.getReplyTo()[0], instanceOf(InternetAddress.class));
        InternetAddress internetAddressReplyTo = (InternetAddress) sentMessage.getReplyTo()[0];
        assertThat(internetAddressReplyTo.getAddress(), is(verifiedMailToSend.getFrom().getEmail()));
        assertThat(internetAddressReplyTo.getPersonal(),
                is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null)));
    } else {
        assertThat(sentMessage.getHeader("Reply-To"), nullValue());
        assertThat(sentMessage.getReplyTo()[0].toString(), is(internetAddressFrom.toString()));
    }
}

From source file:com.cubusmail.user.UserAccountDao.java

/**
 * @param addresses/*from  w  w  w  .  j  av a 2  s .  co  m*/
 */
public void saveRecipients(UserAccount account, InternetAddress[] addresses) {

    ContactFolder recipientFolder = getRecipientContactFolder(account);

    if (recipientFolder == null) {
        recipientFolder = new ContactFolder(ContactFolderType.RECIPIENTS);
        recipientFolder.setFolderName("Recipients");
        recipientFolder.setUserAccount(account);
        saveContactFolder(recipientFolder);
    }
    if (addresses != null) {
        for (InternetAddress address : addresses) {
            Contact contact = getContactByEmail(recipientFolder, address.getAddress());
            if (contact == null) {
                contact = new Contact();
                contact.setLastName(address.getPersonal());
                contact.setEmail(address.getAddress());
                contact.setContactFolder(recipientFolder);
                saveContact(contact);
            }
        }
    }
}

From source file:org.nuxeo.ecm.platform.mail.listener.action.ExtractMessageInformationAction.java

@Override
public boolean execute(ExecutionContext context) {
    bodyContent = "";

    boolean copyMessage = Boolean.parseBoolean(Framework.getProperty(COPY_MESSAGE, "false"));

    try {/*ww  w . j a  v  a2 s .  co m*/
        Message originalMessage = context.getMessage();
        if (log.isDebugEnabled()) {
            log.debug("Transforming message, original subject: " + originalMessage.getSubject());
        }

        // fully load the message before trying to parse to
        // override most of server bugs, see
        // http://java.sun.com/products/javamail/FAQ.html#imapserverbug
        Message message;
        if (originalMessage instanceof MimeMessage && copyMessage) {
            message = new MimeMessage((MimeMessage) originalMessage);
            if (log.isDebugEnabled()) {
                log.debug("Transforming message after full load: " + message.getSubject());
            }
        } else {
            // stuck with the original one
            message = originalMessage;
        }

        // Subject
        String subject = message.getSubject();
        if (subject != null) {
            subject = subject.trim();
        }
        if (subject == null || "".equals(subject)) {
            subject = "<Unknown>";
        }
        context.put(SUBJECT_KEY, subject);

        // Sender
        try {
            Address[] from = message.getFrom();
            String sender = null;
            String senderEmail = null;
            if (from != null) {
                Address addr = from[0];
                if (addr instanceof InternetAddress) {
                    InternetAddress iAddr = (InternetAddress) addr;
                    senderEmail = iAddr.getAddress();
                    sender = iAddr.getPersonal() + " <" + senderEmail + ">";
                } else {
                    sender += addr.toString();
                    senderEmail = sender;
                }
            }
            context.put(SENDER_KEY, sender);
            context.put(SENDER_EMAIL_KEY, senderEmail);
        } catch (AddressException ae) {
            // try to parse sender from header instead
            String[] values = message.getHeader("From");
            if (values != null) {
                context.put(SENDER_KEY, values[0]);
            }
        }
        // Sending date
        context.put(SENDING_DATE_KEY, message.getSentDate());

        // Recipients
        try {
            Address[] to = message.getRecipients(Message.RecipientType.TO);
            Collection<String> recipients = new ArrayList<String>();
            if (to != null) {
                for (Address addr : to) {
                    if (addr instanceof InternetAddress) {
                        InternetAddress iAddr = (InternetAddress) addr;
                        if (iAddr.getPersonal() != null) {
                            recipients.add(iAddr.getPersonal() + " <" + iAddr.getAddress() + ">");
                        } else {
                            recipients.add(iAddr.getAddress());
                        }
                    } else {
                        recipients.add(addr.toString());
                    }
                }
            }
            context.put(RECIPIENTS_KEY, recipients);
        } catch (AddressException ae) {
            // try to parse recipient from header instead
            Collection<String> recipients = getHeaderValues(message, Message.RecipientType.TO.toString());
            context.put(RECIPIENTS_KEY, recipients);
        }

        // CC recipients

        try {
            Address[] toCC = message.getRecipients(Message.RecipientType.CC);
            Collection<String> ccRecipients = new ArrayList<String>();
            if (toCC != null) {
                for (Address addr : toCC) {
                    if (addr instanceof InternetAddress) {
                        InternetAddress iAddr = (InternetAddress) addr;
                        ccRecipients.add(iAddr.getPersonal() + " " + iAddr.getAddress());
                    } else {
                        ccRecipients.add(addr.toString());
                    }
                }
            }
            context.put(CC_RECIPIENTS_KEY, ccRecipients);

        } catch (AddressException ae) {
            // try to parse ccRecipient from header instead
            Collection<String> ccRecipients = getHeaderValues(message, Message.RecipientType.CC.toString());
            context.put(CC_RECIPIENTS_KEY, ccRecipients);
        }
        String[] messageIdHeader = message.getHeader("Message-ID");
        if (messageIdHeader != null) {
            context.put(MailCoreConstants.MESSAGE_ID_KEY, messageIdHeader[0]);
        }

        MimetypeRegistry mimeService = (MimetypeRegistry) context.getInitialContext().get(MIMETYPE_SERVICE_KEY);

        List<Blob> blobs = new ArrayList<Blob>();
        context.put(ATTACHMENTS_KEY, blobs);

        // String[] cte = message.getHeader("Content-Transfer-Encoding");

        // process content
        getAttachmentParts(message, subject, mimeService, context);

        context.put(TEXT_KEY, bodyContent);

        return true;
    } catch (MessagingException | IOException e) {
        log.error(e, e);
    }
    return false;
}