Search in sources :

Example 86 with InternetAddress

use of javax.mail.internet.InternetAddress in project Asqatasun by Asqatasun.

the class EmailSender method sendEmail.

/**
     *
     * @param emailFrom
     * @param emailToSet
     * @param emailBccSet (can be null)
     * @param replyTo (can be null)
     * @param emailSubject
     * @param emailContent
     */
public void sendEmail(String emailFrom, Set<String> emailToSet, Set<String> emailBccSet, String replyTo, String emailSubject, String emailContent) {
    boolean debug = false;
    // Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    // create some properties and get the default Session
    Session session = Session.getInstance(props);
    session.setDebug(debug);
    try {
        Transport t = session.getTransport("smtp");
        t.connect(smtpHost, userName, password);
        // create a message
        MimeMessage msg = new MimeMessage(session);
        // set the from and to address
        InternetAddress addressFrom;
        try {
            // Used default from address is passed one is null or empty or
            // blank
            addressFrom = (StringUtils.isNotBlank(emailFrom)) ? new InternetAddress(emailFrom) : new InternetAddress(from);
            msg.setFrom(addressFrom);
            Address[] recipients = new InternetAddress[emailToSet.size()];
            int i = 0;
            for (String emailTo : emailToSet) {
                recipients[i] = new InternetAddress(emailTo);
                i++;
            }
            msg.setRecipients(Message.RecipientType.TO, recipients);
            if (CollectionUtils.isNotEmpty(emailBccSet)) {
                Address[] bccRecipients = new InternetAddress[emailBccSet.size()];
                i = 0;
                for (String emailBcc : emailBccSet) {
                    bccRecipients[i] = new InternetAddress(emailBcc);
                    i++;
                }
                msg.setRecipients(Message.RecipientType.BCC, bccRecipients);
            }
            if (StringUtils.isNotBlank(replyTo)) {
                Address[] replyToRecipients = { new InternetAddress(replyTo) };
                msg.setReplyTo(replyToRecipients);
            }
            // Setting the Subject
            msg.setSubject(emailSubject, CHARSET_KEY);
            // Setting content and charset (warning: both declarations of
            // charset are needed)
            msg.setHeader(CONTENT_TYPE_KEY, FULL_CHARSET_KEY);
            LOGGER.debug("emailContent  " + emailContent);
            msg.setContent(emailContent, FULL_CHARSET_KEY);
            try {
                LOGGER.debug("emailContent from message object " + msg.getContent().toString());
            } catch (IOException ex) {
                LOGGER.error(ex.getMessage());
            } catch (MessagingException ex) {
                LOGGER.error(ex.getMessage());
            }
            for (Address addr : msg.getAllRecipients()) {
                LOGGER.debug("addr " + addr);
            }
            t.sendMessage(msg, msg.getAllRecipients());
        } catch (AddressException ex) {
            LOGGER.warn("AddressException " + ex.getMessage());
            LOGGER.warn("AddressException " + ex.getStackTrace());
        }
    } catch (NoSuchProviderException e) {
        LOGGER.warn("NoSuchProviderException " + e.getMessage());
        LOGGER.warn("NoSuchProviderException " + e.getStackTrace());
    } catch (MessagingException e) {
        LOGGER.warn("MessagingException " + e.getMessage());
        LOGGER.warn("MessagingException " + e.getStackTrace());
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) InternetAddress(javax.mail.internet.InternetAddress) IOException(java.io.IOException) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException)

Example 87 with InternetAddress

use of javax.mail.internet.InternetAddress in project ats-framework by Axway.

the class MimePackage method addRecipient.

/**
     * Add recipients of a specified type
     *
     * @param type the recipients' type
     * @param addresses the email addresses of the recipients
     * @throws PackageException
     */
@PublicAtsApi
public void addRecipient(RecipientType type, String[] addresses) throws PackageException {
    try {
        // add the recipient
        InternetAddress[] address = new InternetAddress[addresses.length];
        for (int i = 0; i < addresses.length; i++) address[i] = new InternetAddress(addresses[i]);
        message.addRecipients(type.toJavamailType(), address);
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 88 with InternetAddress

use of javax.mail.internet.InternetAddress in project ats-framework by Axway.

the class MimePackage method setRecipient.

/**
     * Set the specified type of recipients of a mime package
     *
     * @param type the recipients' type
     * @param address the email addresses of the recipients
     * @throws PackageException
     */
@PublicAtsApi
public void setRecipient(RecipientType type, String[] addresses) throws PackageException {
    try {
        // add the recipient
        InternetAddress[] address = new InternetAddress[addresses.length];
        for (int i = 0; i < addresses.length; i++) address[i] = new InternetAddress(addresses[i]);
        message.setRecipients(type.toJavamailType(), address);
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 89 with InternetAddress

use of javax.mail.internet.InternetAddress in project ats-framework by Axway.

the class MimePackage method setRecipient.

/**
     * Set the To recipient of a mime package, the CC and BCC recipients are
     * cleared
     *
     * @param address the email address of the recipient
     * @throws PackageException
     */
@PublicAtsApi
public void setRecipient(String address) throws PackageException {
    try {
        // add the recipient
        InternetAddress inetAddress = new InternetAddress(address);
        message.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO, new InternetAddress[] { inetAddress });
        message.setRecipients(javax.mail.internet.MimeMessage.RecipientType.CC, new InternetAddress[] {});
        message.setRecipients(javax.mail.internet.MimeMessage.RecipientType.BCC, new InternetAddress[] {});
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 90 with InternetAddress

use of javax.mail.internet.InternetAddress in project zm-mailbox by Zimbra.

the class FilterUtil method redirect.

public static void redirect(OperationContext octxt, Mailbox sourceMbox, MimeMessage msg, String destinationAddress) throws ServiceException {
    MimeMessage outgoingMsg;
    try {
        if (!isMailLoop(sourceMbox, msg, new String[] { HEADER_FORWARDED })) {
            outgoingMsg = new Mime.FixedMimeMessage(msg);
            Mime.recursiveRepairTransferEncoding(outgoingMsg);
            outgoingMsg.addHeader(HEADER_FORWARDED, sourceMbox.getAccount().getName());
            outgoingMsg.saveChanges();
        } else {
            String error = String.format("Detected a mail loop for message %s.", Mime.getMessageID(msg));
            throw ServiceException.FAILURE(error, null);
        }
    } catch (MessagingException e) {
        try {
            outgoingMsg = createRedirectMsgOnError(msg);
            ZimbraLog.filter.info("Message format error detected.  Wrapper class in use.  %s", e.toString());
        } catch (MessagingException again) {
            throw ServiceException.FAILURE("Message format error detected.  Workaround failed.", again);
        }
    } catch (IOException e) {
        try {
            outgoingMsg = createRedirectMsgOnError(msg);
            ZimbraLog.filter.info("Message format error detected.  Wrapper class in use.  %s", e.toString());
        } catch (MessagingException me) {
            throw ServiceException.FAILURE("Message format error detected.  Workaround failed.", me);
        }
    }
    MailSender sender = sourceMbox.getMailSender().setSaveToSent(false).setRedirectMode(true).setSkipHeaderUpdate(true);
    try {
        if (Provisioning.getInstance().getLocalServer().isMailRedirectSetEnvelopeSender()) {
            if (isDeliveryStatusNotification(msg) && LC.filter_null_env_sender_for_dsn_redirect.booleanValue()) {
                sender.setEnvelopeFrom("<>");
                sender.setDsnNotifyOptions(MailSender.DsnNotifyOption.NEVER);
            } else {
                // Set envelope sender to the account name (bug 31309).
                Account account = sourceMbox.getAccount();
                sender.setEnvelopeFrom(account.getName());
            }
        } else {
            Address from = ArrayUtil.getFirstElement(outgoingMsg.getFrom());
            if (from != null) {
                String address = ((InternetAddress) from).getAddress();
                sender.setEnvelopeFrom(address);
            }
        }
        sender.setRecipients(destinationAddress);
        sender.sendMimeMessage(octxt, sourceMbox, outgoingMsg);
    } catch (MessagingException e) {
        ZimbraLog.filter.warn("Envelope sender will be set to the default value.", e);
    }
}
Also used : Mime(com.zimbra.cs.mime.Mime) Account(com.zimbra.cs.account.Account) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MailSender(com.zimbra.cs.mailbox.MailSender) IOException(java.io.IOException)

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