Example usage for javax.mail Address toString

List of usage examples for javax.mail Address toString

Introduction

In this page you can find the example usage for javax.mail Address toString.

Prototype

@Override
public abstract String toString();

Source Link

Document

Return a String representation of this address object.

Usage

From source file:org.xsocket.connection.SimpleSmtpClient.java

public void send(MimeMessage message) throws IOException, MessagingException {

    IBlockingConnection con = new BlockingConnection(host, port);

    // read greeting
    readResponse(con);//from  w w w  .  ja  v a 2 s  .c om

    sendCmd(con, "Helo mailserver");
    readResponse(con);

    if (username != null) {
        String userPassword = new String(
                Base64.encodeBase64(new String("\000" + username + "\000" + password).getBytes()));
        sendCmd(con, "AUTH PLAIN " + userPassword);
        readResponse(con);
    }

    Address sender = message.getFrom()[0];
    sendCmd(con, "Mail From: " + sender.toString());
    readResponse(con);

    Address[] tos = message.getRecipients(RecipientType.TO);
    for (Address to : tos) {
        sendCmd(con, "Rcpt To: " + to.toString());
        readResponse(con);
    }

    sendCmd(con, "Data");
    readResponse(con);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    message.writeTo(os);
    os.close();

    String s = new String(os.toByteArray());
    con.write(s);
    con.write("\r\n.\r\n");

    sendCmd(con, "Quit");
    readResponse(con);

    con.close();
}

From source file:com.angstoverseer.service.mail.MailServiceImpl.java

@Override
public void handleIncomingEmail(InputStream inputStream) {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    try {/*from   w w w  .  j av  a  2s  .c om*/
        MimeMessage mimeMessage = new MimeMessage(session, inputStream);

        Object messageContent = mimeMessage.getContent();
        String message;
        if (messageContent instanceof Multipart) {
            Multipart mp = (Multipart) messageContent;
            BodyPart bodyPart = mp.getBodyPart(0);
            InputStream is = bodyPart.getInputStream();
            message = convertStreamToString(is);
        } else {
            message = (String) messageContent;
        }

        Address sender = mimeMessage.getFrom()[0];
        final String commandOutput = commandService.process(message, extractEmail(sender.toString()));

        sendResponseMail(sender, commandOutput, mimeMessage.getSubject());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jaeksoft.searchlib.parser.EmlParser.java

@Override
protected void parseContent(StreamLimiter streamLimiter, LanguageEnum lang)
        throws IOException, SearchLibException {
    Session session = Session.getDefaultInstance(JAVAMAIL_PROPS);
    try {//w  w  w . jav  a 2s . c  om

        MimeMessage mimeMessage = new MimeMessage(session, streamLimiter.getNewInputStream());
        MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse();

        ParserResultItem result = getNewParserResultItem();
        String from = mimeMessageParser.getFrom();
        if (from != null)
            result.addField(ParserFieldEnum.email_display_from, from.toString());
        for (Address address : mimeMessageParser.getTo())
            result.addField(ParserFieldEnum.email_display_to, address.toString());
        for (Address address : mimeMessageParser.getCc())
            result.addField(ParserFieldEnum.email_display_cc, address.toString());
        for (Address address : mimeMessageParser.getBcc())
            result.addField(ParserFieldEnum.email_display_bcc, address.toString());
        result.addField(ParserFieldEnum.subject, mimeMessageParser.getSubject());
        result.addField(ParserFieldEnum.htmlSource, mimeMessageParser.getHtmlContent());
        result.addField(ParserFieldEnum.content, mimeMessageParser.getPlainContent());
        result.addField(ParserFieldEnum.email_sent_date, mimeMessage.getSentDate());
        result.addField(ParserFieldEnum.email_received_date, mimeMessage.getReceivedDate());

        for (DataSource dataSource : mimeMessageParser.getAttachmentList()) {
            result.addField(ParserFieldEnum.email_attachment_name, dataSource.getName());
            result.addField(ParserFieldEnum.email_attachment_type, dataSource.getContentType());
            if (parserSelector == null)
                continue;
            Parser attachParser = parserSelector.parseStream(getSourceDocument(), dataSource.getName(),
                    dataSource.getContentType(), null, dataSource.getInputStream(), null, null, null);
            if (attachParser == null)
                continue;
            List<ParserResultItem> parserResults = attachParser.getParserResults();
            if (parserResults != null)
                for (ParserResultItem parserResult : parserResults)
                    result.addField(ParserFieldEnum.email_attachment_content, parserResult);
        }
        if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent()))
            result.langDetection(10000, ParserFieldEnum.content);
        else
            result.langDetection(10000, ParserFieldEnum.htmlSource);
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:com.qwazr.library.email.EmlParser.java

@Override
public void parseContent(final MultivaluedMap<String, String> parameters, final InputStream inputStream,
        final String extension, final String mimeType, final ParserResultBuilder resultBuilder)
        throws Exception {
    final Session session = Session.getDefaultInstance(JAVAMAIL_PROPS);

    resultBuilder.metas().set(MIME_TYPE, findMimeType(extension, mimeType, this::findMimeTypeUsingDefault));

    final MimeMessage mimeMessage = new MimeMessage(session, inputStream);
    final MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse();

    ParserFieldsBuilder document = resultBuilder.newDocument();
    final String from = mimeMessageParser.getFrom();
    if (from != null)
        document.add(FROM, from);//from  w  w w.ja  va2 s .c  om
    for (Address address : mimeMessageParser.getTo())
        document.add(RECIPIENT_TO, address.toString());
    for (Address address : mimeMessageParser.getCc())
        document.add(RECIPIENT_CC, address.toString());
    for (Address address : mimeMessageParser.getBcc())
        document.add(RECIPIENT_BCC, address.toString());
    document.add(SUBJECT, mimeMessageParser.getSubject());
    document.add(HTML_CONTENT, mimeMessageParser.getHtmlContent());
    document.add(PLAIN_CONTENT, mimeMessageParser.getPlainContent());
    document.add(SENT_DATE, mimeMessage.getSentDate());
    document.add(RECEIVED_DATE, mimeMessage.getReceivedDate());

    for (DataSource dataSource : mimeMessageParser.getAttachmentList()) {
        document.add(ATTACHMENT_NAME, dataSource.getName());
        document.add(ATTACHMENT_TYPE, dataSource.getContentType());
        // TODO Extract content from attachmend
        // if (parserSelector != null) {
        // Parser attachParser = parserSelector.parseStream(
        // getSourceDocument(), dataSource.getName(),
        // dataSource.getContentType(), null,
        // dataSource.getInputStream(), null, null, null);
        // if (attachParser != null) {
        // List<ParserResultItem> parserResults = attachParser
        // .getParserResults();
        // if (parserResults != null)
        // for (ParserResultItem parserResult : parserResults)
        // result.addField(
        // ParserFieldEnum.email_attachment_content,
        // parserResult);
        // }
        // }
    }
    if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent()))
        document.add(LANG_DETECTION, languageDetection(document, PLAIN_CONTENT, 10000));
    else
        document.add(LANG_DETECTION, languageDetection(document, HTML_CONTENT, 10000));
}

From source file:com.opensearchserver.extractor.parser.Eml.java

@Override
protected void parseContent(InputStream inputStream, String extension, String mimeType) throws Exception {
    Session session = Session.getDefaultInstance(JAVAMAIL_PROPS);

    MimeMessage mimeMessage = new MimeMessage(session, inputStream);
    MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse();

    ParserDocument document = getNewParserDocument();
    String from = mimeMessageParser.getFrom();
    if (from != null)
        document.add(FROM, from.toString());
    for (Address address : mimeMessageParser.getTo())
        document.add(RECIPIENT_TO, address.toString());
    for (Address address : mimeMessageParser.getCc())
        document.add(RECIPIENT_CC, address.toString());
    for (Address address : mimeMessageParser.getBcc())
        document.add(RECIPIENT_BCC, address.toString());
    document.add(SUBJECT, mimeMessageParser.getSubject());
    document.add(HTML_CONTENT, mimeMessageParser.getHtmlContent());
    document.add(PLAIN_CONTENT, mimeMessageParser.getPlainContent());
    document.add(SENT_DATE, mimeMessage.getSentDate());
    document.add(RECEIVED_DATE, mimeMessage.getReceivedDate());

    for (DataSource dataSource : mimeMessageParser.getAttachmentList()) {
        document.add(ATTACHMENT_NAME, dataSource.getName());
        document.add(ATTACHMENT_TYPE, dataSource.getContentType());
        // TODO Extract content from attachmend
        // if (parserSelector != null) {
        // Parser attachParser = parserSelector.parseStream(
        // getSourceDocument(), dataSource.getName(),
        // dataSource.getContentType(), null,
        // dataSource.getInputStream(), null, null, null);
        // if (attachParser != null) {
        // List<ParserResultItem> parserResults = attachParser
        // .getParserResults();
        // if (parserResults != null)
        // for (ParserResultItem parserResult : parserResults)
        // result.addField(
        // ParserFieldEnum.email_attachment_content,
        // parserResult);
        // }//w  w  w .  j a  va2  s.c o m
        // }
    }
    if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent()))
        document.add(LANG_DETECTION, languageDetection(document, PLAIN_CONTENT, 10000));
    else
        document.add(LANG_DETECTION, languageDetection(document, HTML_CONTENT, 10000));
}

From source file:com.joshlong.lazyblogger.integrations.EmailToBlogTransformer.java

private String emailFromAddress(Address address) {
    if (address instanceof InternetAddress) {
        InternetAddress iaInternetAddress = (InternetAddress) address;
        return iaInternetAddress.getAddress();
    }//from  w ww  .  j  a  v a  2s  .co m
    return address.toString();
}

From source file:hudson.maven.reporters.MavenMailerTest.java

private void assertContainsRecipient(String email, Message message) throws Exception {
    assert email != null;
    assert !email.trim().equals("");
    boolean containRecipient = false;
    for (Address address : message.getAllRecipients()) {
        if (email.equals(address.toString())) {
            containRecipient = true;//  w  ww.  j a va 2 s  .co m
            break;
        }
    }
    assert containRecipient;
}

From source file:org.appverse.web.framework.backend.api.services.integration.impl.live.MailIntegrationServiceImpl.java

@Override
@SuppressWarnings("unchecked")
public void sendMail(String from, String[] to, String[] copyTo, String subject, String templateLocation,
        Map model) throws Exception {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);/*from w  w  w  .  ja va2  s. co m*/
    helper.setTo(to);
    if (copyTo != null) {
        helper.setCc(copyTo);
    }
    helper.setSubject(subject);
    String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, ENCODING, templateLocation,
            model);
    helper.setText(text, true);
    try {
        this.mailSender.send(message);
    } catch (MailSendException sendMailException) {
        Exception[] messageExceptions = sendMailException.getMessageExceptions();
        for (Exception messageException : messageExceptions) {
            if (messageException instanceof SendFailedException) {
                SendFailedException sendFailedException = (SendFailedException) messageException;
                if (sendFailedException.getMessage().equals(INVALID_ADDRESSES)) {
                    Address[] invalidAddresses = sendFailedException.getInvalidAddresses();
                    List<String> invalidAddressList = new ArrayList<String>();
                    for (Address invalidAddress : invalidAddresses) {
                        String invalidAddressString = invalidAddress.toString();
                        invalidAddressList.add(invalidAddressString);
                    }
                    List<String> validToAdressesList = new ArrayList<String>();
                    if (to != null && to.length > 0) {
                        for (String address : to) {
                            if (!invalidAddressList.contains(address)) {
                                validToAdressesList.add(address);
                            }
                        }
                    }

                    List<String> validCopyToAdressesList = new ArrayList<String>();
                    if (copyTo != null && copyTo.length > 0) {
                        for (String address : copyTo) {
                            if (!invalidAddressList.contains(address)) {
                                validCopyToAdressesList.add(address);
                            }
                        }
                    }

                    String[] validToAdressesArray = new String[validToAdressesList.size()];
                    validToAdressesList.toArray(validToAdressesArray);
                    helper.setTo(validToAdressesList.toArray(validToAdressesArray));

                    String[] validCopyToAdressesArray = new String[validCopyToAdressesList.size()];
                    validCopyToAdressesList.toArray(validCopyToAdressesArray);
                    helper.setCc(validCopyToAdressesList.toArray(validCopyToAdressesArray));
                    for (String invalidAddress : invalidAddressList) {
                        logger.error("Mail not sended to " + invalidAddress + "due its a invalid address");
                    }
                    this.mailSender.send(message);
                }
            }
        }
    }
}

From source file:org.appverse.web.framework.backend.api.services.integration.impl.live.MailIntegrationServiceImpl.java

@SuppressWarnings("unchecked")
@Override//from ww w  .  j  a v a  2s.  c o  m
public void sendMail(String from, String[] to, String[] copyTo, String subject, String templateLocation,
        Map model, ArrayList<AttachmentDTO> attachments) throws Exception {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    if (copyTo != null) {
        helper.setCc(copyTo);
    }
    helper.setSubject(subject);
    String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, ENCODING, templateLocation,
            model);
    helper.setText(text, true);
    for (AttachmentDTO attachmentDTO : attachments) {
        helper.addAttachment(attachmentDTO.getAttachmentName(), attachmentDTO.getAttachment());
    }
    try {
        this.mailSender.send(message);
    } catch (MailSendException sendMailException) {
        Exception[] messageExceptions = sendMailException.getMessageExceptions();
        for (Exception messageException : messageExceptions) {
            if (messageException instanceof SendFailedException) {
                SendFailedException sendFailedException = (SendFailedException) messageException;
                if (sendFailedException.getMessage().equals(INVALID_ADDRESSES)) {
                    Address[] invalidAddresses = sendFailedException.getInvalidAddresses();
                    List<String> invalidAddressList = new ArrayList<String>();
                    for (Address invalidAddress : invalidAddresses) {
                        String invalidAddressString = invalidAddress.toString();
                        invalidAddressList.add(invalidAddressString);
                    }
                    List<String> validToAdressesList = new ArrayList<String>();
                    if (to != null && to.length > 0) {
                        for (String address : to) {
                            if (!invalidAddressList.contains(address)) {
                                validToAdressesList.add(address);
                            }
                        }
                    }

                    List<String> validCopyToAdressesList = new ArrayList<String>();
                    if (copyTo != null && copyTo.length > 0) {
                        for (String address : copyTo) {
                            if (!invalidAddressList.contains(address)) {
                                validCopyToAdressesList.add(address);
                            }
                        }
                    }

                    String[] validToAdressesArray = new String[validToAdressesList.size()];
                    validToAdressesList.toArray(validToAdressesArray);
                    helper.setTo(validToAdressesList.toArray(validToAdressesArray));

                    String[] validCopyToAdressesArray = new String[validCopyToAdressesList.size()];
                    validCopyToAdressesList.toArray(validCopyToAdressesArray);
                    helper.setCc(validCopyToAdressesList.toArray(validCopyToAdressesArray));
                    for (String invalidAddress : invalidAddressList) {
                        logger.error("Mail not sended to " + invalidAddress + "due its a invalid address");
                    }
                    this.mailSender.send(message);
                }
            }
        }
    }
}

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

public boolean execute(ExecutionContext context) throws MessagingException {
    Message message = context.getMessage();
    if (log.isDebugEnabled()) {
        log.debug("Transforming message" + message.getSubject());
    }//from  w ww . ja v a 2s  .  c o  m
    if (message.getFrom() != null && message.getFrom().length != 0) {
        List<String> contributors = new ArrayList<String>();
        for (Address ad : message.getFrom()) {
            contributors.add(safelyDecodeText(ad.toString()));
        }
        dcSchema.put("contributors", contributors);
        dcSchema.put("creator", contributors.get(0));
        dcSchema.put("created", message.getReceivedDate());
    }
    if (message.getAllRecipients() != null && message.getAllRecipients().length != 0) {
        List<String> recipients = new ArrayList<String>();
        for (Address address : message.getAllRecipients()) {
            recipients.add(safelyDecodeText(address.toString()));
        }
        mailSchema.put("recipients", recipients);
    }
    if (message instanceof MimeMessage) {
        try {
            processMimeMessage((MimeMessage) message);
        } catch (IOException e) {
            throw new MessagingException(e.getMessage(), e);
        }
    }
    mailSchema.put("text", text.toString());
    dcSchema.put("title", message.getSubject());
    context.put("transformed", schemas);
    return true;
}