props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.socketFactory.port", smtpPort);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
}
);
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
msg.setReplyTo(new Address[]{addressFrom});
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
//msg.setRecipients(Message.RecipientType.BCC, addressTo);
MimeMultipart multipart = new MimeMultipart("alternative");
if(attachmentURL!=null) {
MimeBodyPart filePart = new MimeBodyPart();
filePart.setFileName( attachmentFileName );
URL url = new URL(attachmentURL);
URLConnection connection = url.openConnection();
connection.setRequestProperty( "User-Agent","OpenForum");
ByteArrayDataSource ds = new ByteArrayDataSource(connection.getInputStream(), attachmentMimeType);
filePart.setDataHandler(new DataHandler(ds));
multipart.addBodyPart(filePart);
}
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(textMessage, "text/plain");
multipart.addBodyPart(textPart);
if(htmlMessage!=null) {
MimeBodyPart htmlPart = new MimeBodyPart();
String htmlData = htmlMessage;
htmlPart.setContent(htmlData, "text/html");
multipart.addBodyPart(htmlPart);
}
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(multipart);
Transport transport = session.getTransport(addressTo[0]);
transport.addTransportListener(this);
transport.connect();
// Sending Message
for(Address to: addressTo) {
try{