use of org.mycore.common.MCRMailer.EMail.MessagePart in project mycore by MyCoRe-Org.
the class MCRMailer method send.
/**
* Sends email. When sending email fails (for example, outgoing mail server
* is not responding), sending will be retried after five minutes. This is
* done up to 10 times.
*
* @param from
* the sender of the email
* @param replyTo
* the reply-to addresses as a List of Strings, may be null
* @param to
* the recipients of the email as a List of Strings
* @param bcc
* the bcc recipients of the email as a List of Strings, may be
* null
* @param subject
* the subject of the email
* @param body
* the text of the email
* @param parts
* a List of URL strings which should be added as parts, may be
* null
*/
public static void send(final String from, final List<String> replyTo, final List<String> to, final List<String> bcc, final String subject, final String body, final List<String> parts) {
EMail mail = new EMail();
mail.from = from;
mail.replyTo = replyTo;
mail.to = to;
mail.bcc = bcc;
mail.subject = subject;
mail.msgParts = new ArrayList<>();
mail.msgParts.add(new MessagePart(body));
mail.parts = parts;
send(mail);
}
use of org.mycore.common.MCRMailer.EMail.MessagePart in project mycore by MyCoRe-Org.
the class MCRMailer method trySending.
private static void trySending(EMail mail) throws Exception {
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom(EMail.buildAddress(mail.from));
Optional<List<InternetAddress>> toList = EMail.buildAddressList(mail.to);
if (toList.isPresent())
msg.addRecipients(Message.RecipientType.TO, toList.get().toArray(new InternetAddress[toList.get().size()]));
Optional<List<InternetAddress>> replyToList = EMail.buildAddressList(mail.replyTo);
if (replyToList.isPresent())
msg.setReplyTo((replyToList.get().toArray(new InternetAddress[replyToList.get().size()])));
Optional<List<InternetAddress>> bccList = EMail.buildAddressList(mail.bcc);
if (bccList.isPresent())
msg.addRecipients(Message.RecipientType.BCC, bccList.get().toArray(new InternetAddress[bccList.get().size()]));
msg.setSentDate(new Date());
msg.setSubject(mail.subject, encoding);
if (mail.parts != null && !mail.parts.isEmpty() || mail.msgParts != null && mail.msgParts.size() > 1) {
Multipart multipart = new MimeMultipart();
// Create the message part
MimeBodyPart messagePart = new MimeBodyPart();
if (mail.msgParts.size() > 1) {
multipart = new MimeMultipart("mixed");
MimeMultipart alternative = new MimeMultipart("alternative");
for (MessagePart m : mail.msgParts) {
messagePart = new MimeBodyPart();
messagePart.setText(m.message, encoding, m.type.value());
alternative.addBodyPart(messagePart);
}
messagePart = new MimeBodyPart();
messagePart.setContent(alternative);
multipart.addBodyPart(messagePart);
} else {
Optional<MessagePart> plainMsg = mail.getTextMessage();
if (plainMsg.isPresent()) {
messagePart.setText(plainMsg.get().message, encoding);
multipart.addBodyPart(messagePart);
}
}
if (mail.parts != null && !mail.parts.isEmpty()) {
for (String part : mail.parts) {
messagePart = new MimeBodyPart();
URL url = new URL(part);
DataSource source = new URLDataSource(url);
messagePart.setDataHandler(new DataHandler(source));
String fileName = url.getPath();
if (fileName.contains("\\")) {
fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
} else if (fileName.contains("/")) {
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
}
messagePart.setFileName(fileName);
multipart.addBodyPart(messagePart);
}
}
msg.setContent(multipart);
} else {
Optional<MessagePart> plainMsg = mail.getTextMessage();
if (plainMsg.isPresent()) {
msg.setText(plainMsg.get().message, encoding);
}
}
LOGGER.info("Sending e-mail to {}", mail.to);
Transport.send(msg);
}
Aggregations