String to = "sender@yandex.ru"; // sender email
String from = "receiver@gmail.com"; // receiver email
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "smtp.yandex.ru");
properties.setProperty("mail.smtp.port", "465");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.port", "465");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(properties,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sender@yandex.ru","senderPasswd");
}
}); // default session
try {
MimeMessage message = new MimeMessage(session); // email message
message.setFrom(new InternetAddress(from)); // setting header fields
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test Mail from Java Program"); // subject line
// actual mail body
message.setText("Some Message");
// Send message
Transport.send(message); System.out.println("Email Sent successfully....");
} catch (MessagingException mex){ mex.printStackTrace(); }