use of javax.mail.Multipart in project oxCore by GluuFederation.
the class MailService method sendMail.
public boolean sendMail(SmtpConfiguration mailSmtpConfiguration, String from, String fromDisplayName, String to, String toDisplayName, String subject, String message, String htmlMessage) {
if (mailSmtpConfiguration == null) {
log.error("Failed to send message from '{}' to '{}' because the SMTP configuration isn't valid!", from, to);
return false;
}
log.debug("Host name: " + mailSmtpConfiguration.getHost() + ", port: " + mailSmtpConfiguration.getPort() + ", connection time out: " + this.connectionTimeout);
String mailFrom = from;
if (StringHelper.isEmpty(mailFrom)) {
mailFrom = mailSmtpConfiguration.getFromEmailAddress();
}
String mailFromName = fromDisplayName;
if (StringHelper.isEmpty(mailFromName)) {
mailFromName = mailSmtpConfiguration.getFromName();
}
Properties props = new Properties();
props.put("mail.smtp.host", mailSmtpConfiguration.getHost());
props.put("mail.smtp.port", mailSmtpConfiguration.getPort());
props.put("mail.from", mailFrom);
props.put("mail.smtp.connectiontimeout", this.connectionTimeout);
props.put("mail.smtp.timeout", this.connectionTimeout);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.ssl.trust", mailSmtpConfiguration.getHost());
if (mailSmtpConfiguration.isRequiresSsl()) {
props.put("mail.smtp.socketFactory.port", mailSmtpConfiguration.getPort());
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
Session session = null;
if (mailSmtpConfiguration.isRequiresAuthentication()) {
props.put("mail.smtp.auth", "true");
final String userName = mailSmtpConfiguration.getUserName();
final String password = mailSmtpConfiguration.getPasswordDecrypted();
session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
} else {
Session.getInstance(props, null);
}
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(mailFrom, mailFromName));
if (StringHelper.isEmpty(toDisplayName)) {
msg.setRecipients(Message.RecipientType.TO, to);
} else {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, toDisplayName));
}
msg.setSubject(subject, "UTF-8");
msg.setSentDate(new Date());
if (StringHelper.isEmpty(htmlMessage)) {
msg.setText(message + "\n", "UTF-8", "plain");
} else {
// Unformatted text version
final MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(message, "UTF-8", "plain");
// HTML version
final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText(htmlMessage, "UTF-8", "html");
// Create the Multipart. Add BodyParts to it.
final Multipart mp = new MimeMultipart("alternative");
mp.addBodyPart(textPart);
mp.addBodyPart(htmlPart);
// Set Multipart as the message's content
msg.setContent(mp);
}
Transport.send(msg);
} catch (Exception ex) {
log.error("Failed to send message", ex);
return false;
}
return true;
}
use of javax.mail.Multipart in project java-docs-samples by GoogleCloudPlatform.
the class MailServlet method sendMultipartMail.
private void sendMultipartMail() {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String msgBody = "...";
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("user@example.com", "Mr. User"));
msg.setSubject("Your Example.com account has been activated");
msg.setText(msgBody);
// [START multipart_example]
// ...
String htmlBody = "";
// ...
byte[] attachmentData = null;
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlBody, "text/html");
mp.addBodyPart(htmlPart);
MimeBodyPart attachment = new MimeBodyPart();
InputStream attachmentDataStream = new ByteArrayInputStream(attachmentData);
attachment.setFileName("manual.pdf");
attachment.setContent(attachmentDataStream, "application/pdf");
mp.addBodyPart(attachment);
msg.setContent(mp);
// [END multipart_example]
Transport.send(msg);
} catch (AddressException e) {
// ...
} catch (MessagingException e) {
// ...
} catch (UnsupportedEncodingException e) {
// ...
}
}
use of javax.mail.Multipart in project streamline by hortonworks.
the class EmailNotifier method getEmailMessage.
/**
* Construct a {@link Message} from the map of message field values
*/
private Message getEmailMessage(Map<String, String> fields) throws MessagingException {
Message msg = new MimeMessage(emailSession);
msg.setFrom(new InternetAddress(fields.get(FIELD_FROM.key)));
InternetAddress[] address = { new InternetAddress(fields.get(FIELD_TO.key)) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(fields.get(FIELD_SUBJECT.key));
msg.setSentDate(new Date());
Multipart content = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(fields.get(FIELD_BODY.key), fields.get(FIELD_CONTENT_TYPE.key));
content.addBodyPart(mimeBodyPart);
msg.setContent(content);
return msg;
}
use of javax.mail.Multipart in project TranskribusCore by Transkribus.
the class SendMail method sendMailSSL.
/**
* Sends a mail via SSL
* @param subject The subject of the mail
* @param messageText The message of the mail
* @param attachment The attachments of the mail - can be empty or null
* @param replyTo The reply to addresses (comma seperated!) - can be empty of null
* @param doAuthentication should be true in most cases! UIBK IT allows to send unlimited mails when set to false
* @throws MessagingException
*/
public void sendMailSSL(String toAddress, String subject, String messageText, File[] attachment, String replyTo, boolean doAuthentication) throws MessagingException {
Session session = createSession(doAuthentication);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(email));
if (replyTo != null && !replyTo.isEmpty())
message.setReplyTo(InternetAddress.parse(replyTo));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject(subject);
message.setSentDate(new Date());
if (attachment == null || attachment.length == 0) {
message.setText(messageText);
} else {
Multipart multipart = createMultiPartMessage(messageText, attachment);
message.setContent(multipart);
}
Transport.send(message);
}
use of javax.mail.Multipart in project OpenOLAT by OpenOLAT.
the class MailManagerImpl method createMimeMessage.
@Override
public MimeMessage createMimeMessage(Address from, Address[] tos, Address[] ccs, Address[] bccs, String subject, String body, List<File> attachments, MailerResult result) {
if (from == null || ((tos == null || tos.length == 0) && ((ccs == null || ccs.length == 0)) && (bccs == null || bccs.length == 0)))
return null;
try {
MimeMessage msg = createMessage(subject, from);
if (tos != null && tos.length > 0) {
msg.addRecipients(RecipientType.TO, tos);
}
if (ccs != null && ccs.length > 0) {
msg.addRecipients(RecipientType.CC, ccs);
}
if (bccs != null && bccs.length > 0) {
msg.addRecipients(RecipientType.BCC, bccs);
}
if (attachments != null && !attachments.isEmpty()) {
// with attachment use multipart message
Multipart multipart = new MimeMultipart("mixed");
// 1) add body part
if (StringHelper.isHtml(body)) {
Multipart alternativePart = createMultipartAlternative(body);
MimeBodyPart wrap = new MimeBodyPart();
wrap.setContent(alternativePart);
multipart.addBodyPart(wrap);
} else {
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
multipart.addBodyPart(messageBodyPart);
}
// 2) add attachments
for (File attachmentFile : attachments) {
// abort if attachment does not exist
if (attachmentFile == null || !attachmentFile.exists()) {
result.setReturnCode(MailerResult.ATTACHMENT_INVALID);
log.error("Tried to send mail wit attachment that does not exist::" + (attachmentFile == null ? null : attachmentFile.getAbsolutePath()), null);
return msg;
}
BodyPart filePart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentFile);
filePart.setDataHandler(new DataHandler(source));
filePart.setFileName(attachmentFile.getName());
multipart.addBodyPart(filePart);
}
// Put parts in message
msg.setContent(multipart);
} else {
// without attachment everything is easy, just set as text
if (StringHelper.isHtml(body)) {
msg.setContent(createMultipartAlternative(body));
} else {
msg.setText(body, "utf-8");
}
}
msg.setSentDate(new Date());
msg.saveChanges();
return msg;
} catch (AddressException e) {
result.setReturnCode(MailerResult.SENDER_ADDRESS_ERROR);
log.error("", e);
return null;
} catch (MessagingException e) {
result.setReturnCode(MailerResult.SEND_GENERAL_ERROR);
log.error("", e);
return null;
} catch (UnsupportedEncodingException e) {
result.setReturnCode(MailerResult.SENDER_ADDRESS_ERROR);
log.error("", e);
return null;
}
}
Aggregations