Search in sources :

Example 16 with InternetAddress

use of javax.mail.internet.InternetAddress in project hudson-2.x by hudson.

the class BaseMailSender method getMail.

/**
     * Returns prepared email.
     *
     * @return prepared email.
     * @throws MessagingException exception if any.
     */
protected MimeMessage getMail() throws MessagingException {
    MimeMessage msg = new HudsonMimeMessage(Mailer.descriptor().createSession());
    msg.setContent("", "text/plain");
    msg.setFrom(new InternetAddress(Mailer.descriptor().getAdminAddress()));
    msg.setSentDate(new Date());
    Set<InternetAddress> rcp = new LinkedHashSet<InternetAddress>();
    StringTokenizer tokens = new StringTokenizer(recipients);
    while (tokens.hasMoreTokens()) {
        String address = tokens.nextToken();
        try {
            rcp.add(new InternetAddress(address));
        } catch (AddressException ignore) {
        // ignore bad address, but try to send to other addresses
        }
    }
    msg.setRecipients(Message.RecipientType.TO, rcp.toArray(new InternetAddress[rcp.size()]));
    msg.setSubject(new StringBuilder().append(getSubjectPrefix()).append(" ").append(getSubject()).toString(), charset);
    msg.setText(new StringBuilder().append(getText()).append(getTextFooter()).toString(), charset);
    return msg;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InternetAddress(javax.mail.internet.InternetAddress) StringTokenizer(java.util.StringTokenizer) HudsonMimeMessage(hudson.tasks.HudsonMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) HudsonMimeMessage(hudson.tasks.HudsonMimeMessage) Date(java.util.Date)

Example 17 with InternetAddress

use of javax.mail.internet.InternetAddress in project ninja by ninjaframework.

the class CommonsMailHelperImplTest method doConvertAdressesToInternetAddressList.

/**
     * Convenience method as commonsmail does not use generics...
     * 
     * @param list
     * @return
     */
private List<InternetAddress> doConvertAdressesToInternetAddressList(List<?> list) {
    List<InternetAddress> returnList = new ArrayList<InternetAddress>();
    for (Object object : list) {
        InternetAddress internetAddress = (InternetAddress) object;
        returnList.add(internetAddress);
    }
    return returnList;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList)

Example 18 with InternetAddress

use of javax.mail.internet.InternetAddress in project ninja by ninjaframework.

the class CommonsmailHelperImpl method createValidEmailFromString.

@Override
public Tuple<String, String> createValidEmailFromString(String email) throws AddressException {
    InternetAddress internetAddress = new InternetAddress(email);
    Tuple<String, String> tuple = new Tuple<String, String>(internetAddress.getAddress(), internetAddress.getPersonal());
    return tuple;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Tuple(ninja.postoffice.common.Tuple)

Example 19 with InternetAddress

use of javax.mail.internet.InternetAddress in project jodd by oblac.

the class SendMailSession method createMessage.

// ---------------------------------------------------------------- adapter
/**
	 * Creates new JavaX message from {@link Email email}.
	 */
protected MimeMessage createMessage(Email email, Session session) throws MessagingException {
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(email.getFrom().toInternetAddress());
    // to
    int totalTo = email.getTo().length;
    InternetAddress[] address = new InternetAddress[totalTo];
    for (int i = 0; i < totalTo; i++) {
        address[i] = email.getTo()[i].toInternetAddress();
    }
    msg.setRecipients(Message.RecipientType.TO, address);
    // replyTo
    if (email.getReplyTo() != null) {
        int totalReplyTo = email.getReplyTo().length;
        address = new InternetAddress[totalReplyTo];
        for (int i = 0; i < totalReplyTo; i++) {
            address[i] = email.getReplyTo()[i].toInternetAddress();
        }
        msg.setReplyTo(address);
    }
    // cc
    if (email.getCc() != null) {
        int totalCc = email.getCc().length;
        address = new InternetAddress[totalCc];
        for (int i = 0; i < totalCc; i++) {
            address[i] = email.getCc()[i].toInternetAddress();
        }
        msg.setRecipients(Message.RecipientType.CC, address);
    }
    // bcc
    if (email.getBcc() != null) {
        int totalBcc = email.getBcc().length;
        address = new InternetAddress[totalBcc];
        for (int i = 0; i < totalBcc; i++) {
            address[i] = email.getBcc()[i].toInternetAddress();
        }
        msg.setRecipients(Message.RecipientType.BCC, address);
    }
    if (email.getSubjectEncoding() != null) {
        msg.setSubject(email.getSubject(), email.getSubjectEncoding());
    } else {
        msg.setSubject(email.getSubject());
    }
    Date date = email.getSentDate();
    if (date == null) {
        date = new Date();
    }
    msg.setSentDate(date);
    // headers
    Map<String, String> headers = email.getAllHeaders();
    if (headers != null) {
        for (Map.Entry<String, String> stringStringEntry : headers.entrySet()) {
            String value = stringStringEntry.getValue();
            msg.setHeader(stringStringEntry.getKey(), value);
        }
    }
    // message data and attachments
    final List<EmailMessage> messages = email.getAllMessages();
    final List<EmailAttachment> attachments = email.getAttachments() == null ? null : new ArrayList<>(email.getAttachments());
    final int totalMessages = messages.size();
    if ((attachments == null) && (totalMessages == 1)) {
        // special case: no attachments and just one content
        EmailMessage emailMessage = messages.get(0);
        msg.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
    } else {
        Multipart multipart = new MimeMultipart();
        Multipart msgMultipart = multipart;
        if (totalMessages > 1) {
            MimeBodyPart bodyPart = new MimeBodyPart();
            msgMultipart = new MimeMultipart(ALTERNATIVE);
            bodyPart.setContent(msgMultipart);
            multipart.addBodyPart(bodyPart);
        }
        for (EmailMessage emailMessage : messages) {
            // detect embedded attachments
            List<EmailAttachment> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage);
            MimeBodyPart bodyPart = new MimeBodyPart();
            if (embeddedAttachments == null) {
                // no embedded attachments, just add message
                bodyPart.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
            } else {
                // embedded attachments detected, join them as related
                MimeMultipart relatedMultipart = new MimeMultipart(RELATED);
                MimeBodyPart messageData = new MimeBodyPart();
                messageData.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
                relatedMultipart.addBodyPart(messageData);
                for (EmailAttachment att : embeddedAttachments) {
                    MimeBodyPart attBodyPart = createAttachmentBodyPart(att);
                    relatedMultipart.addBodyPart(attBodyPart);
                }
                bodyPart.setContent(relatedMultipart);
            }
            msgMultipart.addBodyPart(bodyPart);
        }
        if (attachments != null) {
            // attach remaining attachments
            for (EmailAttachment att : attachments) {
                MimeBodyPart attBodyPart = createAttachmentBodyPart(att);
                multipart.addBodyPart(attBodyPart);
            }
        }
        msg.setContent(multipart);
    }
    return msg;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Map(java.util.Map)

Example 20 with InternetAddress

use of javax.mail.internet.InternetAddress in project jodd by oblac.

the class RFC2822AddressParser method pullFromGroups.

// ---------------------------------------------------------------- utilities
private InternetAddress pullFromGroups(Matcher m) {
    InternetAddress currentInternetAddress;
    String[] parts = _calcMatcherParts(m);
    if (parts[1] == null || parts[2] == null) {
        return null;
    }
    // functions use pullFromGroups; some call getMatcherParts directly.
    try {
        //currentInternetAddress = new InternetAddress(parts[0] + " <" + parts[1] + "@" +
        //                                 parts[2]+ ">", true);
        // so it parses it OK, but since javamail doesn't extract too well
        // we make sure that the consistent parts
        // are correct
        currentInternetAddress = new InternetAddress();
        currentInternetAddress.setPersonal(parts[0]);
        currentInternetAddress.setAddress(parts[1] + "@" + parts[2]);
    } catch (UnsupportedEncodingException uee) {
        currentInternetAddress = null;
    }
    return currentInternetAddress;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

InternetAddress (javax.mail.internet.InternetAddress)255 MimeMessage (javax.mail.internet.MimeMessage)106 MessagingException (javax.mail.MessagingException)69 Session (javax.mail.Session)49 Properties (java.util.Properties)45 ArrayList (java.util.ArrayList)42 Address (javax.mail.Address)41 Message (javax.mail.Message)40 Date (java.util.Date)38 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)36 AddressException (javax.mail.internet.AddressException)34 X509Certificate (java.security.cert.X509Certificate)32 MimeBodyPart (javax.mail.internet.MimeBodyPart)30 Test (org.junit.Test)29 IOException (java.io.IOException)26 MimeMultipart (javax.mail.internet.MimeMultipart)26 PolicyExpression (org.nhindirect.policy.PolicyExpression)18 HashMap (java.util.HashMap)17 CertificateResolver (org.nhindirect.stagent.cert.CertificateResolver)17 PolicyResolver (org.nhindirect.stagent.policy.PolicyResolver)17