props.remove("password");
}
SessionAndTransport sat = SMTPConnectionPool.getSessionAndTransport(props,hash(props),auth);
// 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);
//msg.setRecipients(Message.RecipientType.TO, tos);
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));
} catch (UnsupportedEncodingException e) {
throw new MessagingException("the encoding "+charset+" is not supported");
}
msg.setHeader("X-Mailer", xmailer);
msg.setHeader("Date",getNow(timeZone));
//msg.setSentDate(new Date());
Multipart mp=null;
// Only HTML
if(plainText==null) {
if(ArrayUtil.isEmpty(attachmentz) && ArrayUtil.isEmpty(parts)){
fillHTMLText(msg);
setHeaders(msg,headers);
return new MimeMessageAndSession(msg,sat);
}
mp = new MimeMultipart();
mp.addBodyPart(getHTMLText());
}
// only Plain
else if(htmlText==null) {
if(ArrayUtil.isEmpty(attachmentz) && ArrayUtil.isEmpty(parts)){
fillPlainText(msg);
setHeaders(msg,headers);
return new MimeMessageAndSession(msg,sat);
}
mp = new MimeMultipart();
mp.addBodyPart(getPlainText());
}
// Plain and HTML
else {
mp=new MimeMultipart("alternative");
mp.addBodyPart(getPlainText());
mp.addBodyPart(getHTMLText());
if(!ArrayUtil.isEmpty(attachmentz) || !ArrayUtil.isEmpty(parts)){
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp);
mp = new MimeMultipart();
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(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);
}