use of lucee.runtime.net.mail.MailPart in project Lucee by lucee.
the class SMTPClient method createMimeMessage.
private MimeMessageAndSession createMimeMessage(lucee.runtime.config.Config config, String hostName, int port, String username, String password, long lifeTimesan, long idleTimespan, boolean tls, boolean ssl, boolean sendPartial, boolean newConnection, boolean userset) throws MessagingException {
Properties props = (Properties) System.getProperties().clone();
String strTimeout = Caster.toString(getTimeout(config));
props.put("mail.smtp.host", hostName);
props.put("mail.smtp.timeout", strTimeout);
props.put("mail.smtp.connectiontimeout", strTimeout);
props.put("mail.smtp.sendpartial", Caster.toString(sendPartial));
props.put("mail.smtp.userset", userset);
if (port > 0) {
props.put("mail.smtp.port", Caster.toString(port));
}
if (ssl) {
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", Caster.toString(port));
props.put("mail.smtp.socketFactory.fallback", "false");
} else {
props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
props.remove("mail.smtp.socketFactory.port");
props.remove("mail.smtp.socketFactory.fallback");
}
Authenticator auth = null;
if (!StringUtil.isEmpty(username)) {
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", tls ? "true" : "false");
props.put("mail.smtp.user", username);
props.put("mail.smtp.password", password);
props.put("password", password);
auth = new SMTPAuthenticator(username, password);
} else {
props.put("mail.smtp.auth", "false");
props.remove("mail.smtp.starttls.enable");
props.remove("mail.smtp.user");
props.remove("mail.smtp.password");
props.remove("password");
}
SessionAndTransport sat = newConnection ? new SessionAndTransport(hash(props), props, auth, lifeTimesan, idleTimespan) : SMTPConnectionPool.getSessionAndTransport(props, hash(props), auth, lifeTimesan, idleTimespan);
// Contacts
SMTPMessage msg = new SMTPMessage(sat.session);
if (from == null)
throw new MessagingException("you have do define the from for the mail");
// if(tos==null)throw new MessagingException("you have do define the to for the mail");
checkAddress(from, charset);
// checkAddress(tos,charset);
msg.setFrom(from);
if (tos != null) {
checkAddress(tos, charset);
msg.setRecipients(Message.RecipientType.TO, tos);
}
if (ccs != null) {
checkAddress(ccs, charset);
msg.setRecipients(Message.RecipientType.CC, ccs);
}
if (bccs != null) {
checkAddress(bccs, charset);
msg.setRecipients(Message.RecipientType.BCC, bccs);
}
if (rts != null) {
checkAddress(rts, charset);
msg.setReplyTo(rts);
}
if (fts != null) {
checkAddress(fts, charset);
msg.setEnvelopeFrom(fts[0].toString());
}
// Subject and headers
try {
msg.setSubject(MailUtil.encode(subject, charset.name()));
} catch (UnsupportedEncodingException e) {
throw new MessagingException("the encoding " + charset + " is not supported");
}
msg.setHeader("X-Mailer", xmailer);
msg.setHeader("Date", getNow(timeZone));
Multipart mp = null;
// only Plain
if (StringUtil.isEmpty(htmlText)) {
if (ArrayUtil.isEmpty(attachmentz) && ArrayUtil.isEmpty(parts)) {
fillPlainText(config, msg);
setHeaders(msg, headers);
return new MimeMessageAndSession(msg, sat);
}
mp = new MimeMultipart("mixed");
mp.addBodyPart(getPlainText(config));
} else // Only HTML
if (StringUtil.isEmpty(plainText)) {
if (ArrayUtil.isEmpty(attachmentz) && ArrayUtil.isEmpty(parts)) {
fillHTMLText(config, msg);
setHeaders(msg, headers);
return new MimeMessageAndSession(msg, sat);
}
mp = new MimeMultipart("mixed");
mp.addBodyPart(getHTMLText(config));
} else // Plain and HTML
{
mp = new MimeMultipart("alternative");
mp.addBodyPart(getPlainText(config));
// this need to be last
mp.addBodyPart(getHTMLText(config));
if (!ArrayUtil.isEmpty(attachmentz) || !ArrayUtil.isEmpty(parts)) {
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp);
mp = new MimeMultipart("mixed");
mp.addBodyPart(content);
}
}
// parts
if (!ArrayUtil.isEmpty(parts)) {
Iterator<MailPart> it = parts.iterator();
if (mp instanceof MimeMultipart)
((MimeMultipart) mp).setSubType("alternative");
while (it.hasNext()) {
mp.addBodyPart(toMimeBodyPart(config, it.next()));
}
}
// Attachments
if (!ArrayUtil.isEmpty(attachmentz)) {
for (int i = 0; i < attachmentz.length; i++) {
mp.addBodyPart(toMimeBodyPart(mp, config, attachmentz[i]));
}
}
msg.setContent(mp);
setHeaders(msg, headers);
return new MimeMessageAndSession(msg, sat);
}
Aggregations