in app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java [568:662]
public static void sendMessage(String from, String[] to, String[] cc, String[] bcc, String subject,
String content, String mimeType) throws MessagingException {
MailProvider mailProvider = WebloggerStartup.getMailProvider();
if (mailProvider == null) {
return;
}
Session session = mailProvider.getSession();
MimeMessage message = new MimeMessage(session);
// n.b. any default from address is expected to be determined by caller.
if (! StringUtils.isEmpty(from)) {
InternetAddress sentFrom = new InternetAddress(from);
message.setFrom(sentFrom);
if (log.isDebugEnabled()) {
log.debug("e-mail from: " + sentFrom);
}
}
if (to!=null) {
InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
sendTo[i] = new InternetAddress(to[i]);
if (log.isDebugEnabled()) {
log.debug("sending e-mail to: " + to[i]);
}
}
message.setRecipients(Message.RecipientType.TO, sendTo);
}
if (cc != null) {
InternetAddress[] copyTo = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
copyTo[i] = new InternetAddress(cc[i]);
if (log.isDebugEnabled()) {
log.debug("copying e-mail to: " + cc[i]);
}
}
message.setRecipients(Message.RecipientType.CC, copyTo);
}
if (bcc != null) {
InternetAddress[] copyTo = new InternetAddress[bcc.length];
for (int i = 0; i < bcc.length; i++) {
copyTo[i] = new InternetAddress(bcc[i]);
if (log.isDebugEnabled()) {
log.debug("blind copying e-mail to: " + bcc[i]);
}
}
message.setRecipients(Message.RecipientType.BCC, copyTo);
}
message.setSubject((subject == null) ? "(no subject)" : subject, "UTF-8");
message.setContent(content, mimeType);
message.setSentDate(new java.util.Date());
// First collect all the addresses together.
Address[] remainingAddresses = message.getAllRecipients();
int nAddresses = remainingAddresses.length;
boolean bFailedToSome = false;
SendFailedException sendex = new SendFailedException("Unable to send message to some recipients");
Transport transport = mailProvider.getTransport();
// Try to send while there remain some potentially good addresses
try {
do {
// Avoid a loop if we are stuck
nAddresses = remainingAddresses.length;
try {
// Send to the list of remaining addresses, ignoring the addresses attached to the message
transport.sendMessage(message, remainingAddresses);
} catch(SendFailedException ex) {
bFailedToSome=true;
sendex.setNextException(ex);
// Extract the remaining potentially good addresses
remainingAddresses=ex.getValidUnsentAddresses();
}
} while (remainingAddresses!=null && remainingAddresses.length>0
&& remainingAddresses.length!=nAddresses);
} finally {
transport.close();
}
if (bFailedToSome) {
throw sendex;
}
}