Do the work: send the mail to the SMTP server.
// We need to pass info to the mail server as a Properties, since
// JavaMail (wisely) allows room for LOTS of properties...
FileProperties props =
new FileProperties(MailConstants.PROPS_FILE_NAME);
// Copy the value of Mail.send.host into mail.smtp.host
props.setProperty("mail.smtp.host",
props.getProperty(MailConstants.SEND_HOST));
// Create the Session object
session = Session.getDefaultInstance(props, null);
session.setDebug(true); // Verbose!
try {
// create a message
mesg = new MimeMessage(session);
// From Address - this should come from a Properties...
mesg.setFrom(new InternetAddress("nobody@host.domain"));
// TO Address
InternetAddress toAddress = new InternetAddress(message_recip);
mesg.addRecipient(Message.RecipientType.TO, toAddress);
// CC Address
InternetAddress ccAddress = new InternetAddress(message_cc);
mesg.addRecipient(Message.RecipientType.CC, ccAddress);
// The Subject
mesg.setSubject(message_subject);
// Now the message body.
Multipart mp = new MimeMultipart();
BodyPart textPart = new MimeBodyPart();
textPart.setText(message_body); // sets type to "text/plain"
BodyPart pixPart = new MimeBodyPart();
pixPart.setContent(html_data, "text/html");
// Collect the Parts into the MultiPart
mp.addBodyPart(textPart);
mp.addBodyPart(pixPart);
// Put the MultiPart into the Message
mesg.setContent(mp);
// Finally, send the message!
Transport.send(mesg);
} catch (MessagingException ex) {
System.err.println(ex);
ex.printStackTrace(System.err);
}