1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| String to = "receiver";
String from = "sender";
String host = "host";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
// Session session = Session.getDefaultInstance(props);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());
// Set message content
msg.setText("This is a test of sending a " +
"plain text e-mail through Java.\n" +
"Here is line 2.");
Transport tr = session.getTransport("smtp");
tr.connect(host,"userName" , "PassWd");
//Send the message
tr.send(msg);
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
} |