/* * Copyright (C) 2003-2008 Jive Software, 2017-2025 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jivesoftware.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.mail.*; import javax.mail.internet.*; import java.security.Security; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.Properties; /** * A service to send email.
* * This class has a few factory methods you can use to return message objects * or to add messages into a queue to be sent. Using these methods, you can * send emails in the following couple of ways: *
* EmailService.sendMessage( * "Joe Bloe", "jbloe@place.org", * "Jane Doe", "jane@doe.com", * "Hello...", * "This is the body of the email...", * null * ); ** or *
* Message message = EmailService.createMimeMessage(); * // call setters on the message object * // . * // . * // . * emailService.sendMessage(message); *
* * This class is configured with a set of Jive properties:
* * To have more advanced control over the message sent, use the * {@link #sendMessage(MimeMessage)} method.
*
* Both a plain text and html body can be specified. If one of the values is null,
* only the other body type is sent. If both body values are set, a multi-part
* message will be sent. If parts of the message are invalid (ie, the toEmail is null)
* the message won't be sent.
*
* @param toName the name of the recipient of this email.
* @param toEmail the email address of the recipient of this email.
* @param fromName the name of the sender of this email.
* @param fromEmail the email address of the sender of this email.
* @param subject the subject of the email.
* @param textBody plain text body of the email, which can be {@code null} if the
* html body is not null.
* @param htmlBody html body of the email, which can be {@code null} if the text body
* is not null.
*/
public void sendMessage(String toName, String toEmail, String fromName,
String fromEmail, String subject, String textBody, String htmlBody)
{
// Check for errors in the given fields:
if (toEmail == null || fromEmail == null || subject == null ||
(textBody == null && htmlBody == null))
{
Log.error("Error sending email: Invalid fields: "
+ ((toEmail == null) ? "toEmail " : "")
+ ((fromEmail == null) ? "fromEmail " : "")
+ ((subject == null) ? "subject " : "")
+ ((textBody == null && htmlBody == null) ? "textBody or htmlBody " : "")
);
}
else {
try {
String encoding = MimeUtility.mimeCharset("UTF-8");
MimeMessage message = createMimeMessage();
Address to;
Address from;
if (toName != null) {
to = new InternetAddress(toEmail, toName, encoding);
}
else {
to = new InternetAddress(toEmail, "", encoding);
}
if (fromName != null) {
from = new InternetAddress(fromEmail, fromName, encoding);
}
else {
from = new InternetAddress(fromEmail, "", encoding);
}
// Set the date of the message to be the current date
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z",
java.util.Locale.US);
format.setTimeZone(JiveGlobals.getTimeZone());
message.setHeader("Date", format.format(new Date()));
message.setHeader("Content-Transfer-Encoding", "8bit");
message.setRecipient(Message.RecipientType.TO, to);
message.setFrom(from);
message.setSubject(subject.replace("\n", ""), encoding);
// Create HTML, plain-text, or combination message
if (textBody != null && htmlBody != null) {
MimeMultipart content = new MimeMultipart("alternative");
// Plain-text
MimeBodyPart text = new MimeBodyPart();
text.setText(textBody, encoding);
text.setDisposition(Part.INLINE);
content.addBodyPart(text);
// HTML
MimeBodyPart html = new MimeBodyPart();
html.setContent(htmlBody, "text/html; charset=UTF-8");
html.setDisposition(Part.INLINE);
html.setHeader("Content-Transfer-Encoding", "8bit");
content.addBodyPart(html);
// Add multipart to message.
message.setContent(content);
message.setDisposition(Part.INLINE);
sendMessage(message);
}
else if (textBody != null) {
MimeBodyPart bPart = new MimeBodyPart();
bPart.setText(textBody, encoding);
bPart.setDisposition(Part.INLINE);
bPart.setHeader("Content-Transfer-Encoding", "8bit");
MimeMultipart mPart = new MimeMultipart();
mPart.addBodyPart(bPart);
message.setContent(mPart);
message.setDisposition(Part.INLINE);
// Add the message to the send list
sendMessage(message);
}
else if (htmlBody != null) {
MimeBodyPart bPart = new MimeBodyPart();
bPart.setContent(htmlBody, "text/html; charset=UTF-8");
bPart.setDisposition(Part.INLINE);
bPart.setHeader("Content-Transfer-Encoding", "8bit");
MimeMultipart mPart = new MimeMultipart();
mPart.addBodyPart(bPart);
message.setContent(mPart);
message.setDisposition(Part.INLINE);
// Add the message to the send list
sendMessage(message);
}
}
catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
}
/**
* Sends a collection of email messages. This method differs from
* {@link #sendMessages(Collection)} in that messages are sent
* before this method returns rather than queueing the messages to be sent later.
*
* @param messages the messages to send.
* @throws MessagingException if an error occurs.
*/
public void sendMessagesImmediately(Collection