public static void main(String[] args) throws IOException {
String [] to = {"lawrence6@gmail.com", "kinr@gmail.com"}; // to address. It can be any like gmail, hotmail etc.
final String from = "hyella@zohomail.com"; // from address. As this is using Gmail SMTP.
final String password = "xxxxxx"; // password for from mail address.
//Set smtp connection attributes
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.zoho.com");
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.startssl.enable", "true");
prop.put("mail.debug", "true");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//create new session with an authenticator
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
};
Session session = Session.getInstance(prop, auth);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of the "to" addresses
for (int i = 0; i < to.length; i++) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
//Set header field of the header
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("CBN ECM JAVA");
String msg = "<h1>This email sent using Java Code!!!</h1>";
msg += "<b>Wish you a nice day!</b><br>";
msg += "<font color=red>Forty-two</font>";
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.attachFile(new File("C://Xampp//successful.JPG"));
multipart.addBodyPart(attachmentBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Mail successfully sent..");
} catch (MessagingException e) {
e.printStackTrace();
}
}
Writer is a powerful online word processor, designed for collaborative work.