in src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java [138:275]
public void send() {
try {
final Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", String.valueOf(port));
// Aside, the JDK is clearly unaware of the Scottish
// 'session', which involves excessive quantities of
// alcohol :-)
Session sesh;
Authenticator auth = null;
if (SSL) {
// SMTP provider
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtps.host", host);
if (isPortExplicitlySpecified()) {
props.put("mail.smtps.port", String.valueOf(port));
props.put("mail.smtp.socketFactory.port",
String.valueOf(port));
}
}
if (user != null || password != null) {
props.put("mail.smtp.auth", "true");
auth = new SimpleAuthenticator(user, password);
}
if (isStartTLSEnabled()) {
props.put("mail.smtp.starttls.enable", "true");
}
sesh = Session.getInstance(props, auth);
//create the message
final MimeMessage msg = new MimeMessage(sesh);
final MimeMultipart attachments = new MimeMultipart();
//set the sender
if (from.getName() == null) {
msg.setFrom(new InternetAddress(from.getAddress()));
} else {
msg.setFrom(new InternetAddress(from.getAddress(),
from.getName()));
}
// set the reply to addresses
msg.setReplyTo(internetAddresses(replyToList));
msg.setRecipients(Message.RecipientType.TO,
internetAddresses(toList));
msg.setRecipients(Message.RecipientType.CC,
internetAddresses(ccList));
msg.setRecipients(Message.RecipientType.BCC,
internetAddresses(bccList));
// Choosing character set of the mail message
// First: looking it from MimeType
String charset = parseCharSetFromMimeType(message.getMimeType());
if (charset != null) {
// Assign/reassign message charset from MimeType
message.setCharset(charset);
} else {
// Next: looking if charset having explicit definition
charset = message.getCharset();
if (charset == null) {
// Using default
charset = DEFAULT_CHARSET;
message.setCharset(charset);
}
}
// Using javax.activation.DataSource paradigm
final StringDataSource sds = new StringDataSource();
sds.setContentType(message.getMimeType());
sds.setCharset(charset);
if (subject != null) {
msg.setSubject(subject, charset);
}
msg.addHeader("Date", getDate());
if (headers != null) {
for (Header h : headers) {
msg.addHeader(h.getName(), h.getValue());
}
}
final PrintStream out = new PrintStream(sds.getOutputStream());
message.print(out);
out.close();
final MimeBodyPart textbody = new MimeBodyPart();
textbody.setDataHandler(new DataHandler(sds));
attachments.addBodyPart(textbody);
for (File file : files) {
MimeBodyPart body = new MimeBodyPart();
if (!file.exists() || !file.canRead()) {
throw new BuildException(
"File \"%s\" does not exist or is not readable.",
file.getAbsolutePath());
}
final FileDataSource fileData = new FileDataSource(file);
final DataHandler fileDataHandler = new DataHandler(fileData);
body.setDataHandler(fileDataHandler);
body.setFileName(file.getName());
attachments.addBodyPart(body);
}
msg.setContent(attachments);
try {
// Send the message using SMTP, or SMTPS if the host uses SSL
final Transport transport = sesh.getTransport(SSL ? "smtps" : "smtp");
transport.connect(host, user, password);
transport.sendMessage(msg, msg.getAllRecipients());
} catch (final SendFailedException sfe) {
if (!shouldIgnoreInvalidRecipients()) {
throw new BuildException(GENERIC_ERROR, sfe);
}
if (sfe.getValidSentAddresses() == null
|| sfe.getValidSentAddresses().length == 0) {
throw new BuildException("Couldn't reach any recipient",
sfe);
}
Address[] invalid = sfe.getInvalidAddresses();
if (invalid == null) {
invalid = new Address[0];
}
for (Address address : invalid) {
didntReach(address, "invalid", sfe);
}
Address[] validUnsent = sfe.getValidUnsentAddresses();
if (validUnsent == null) {
validUnsent = new Address[0];
}
for (Address address : validUnsent) {
didntReach(address, "valid", sfe);
}
}
} catch (MessagingException | IOException e) {
throw new BuildException(GENERIC_ERROR, e);
}
}