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());
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations