How to send an Email using Java Mail API
Download the code (and the mail.jar - JavaMail API) from here;
https://goo.gl/rygvBF
OR you can download the mail.jar - JavaMail API from here;
http://goo.gl/u1b3uO
00:59 - add .jar file
01:33 - run the code
02:32 - code presentation
public static void send(String to, String sub,String msg, final String user, final String pass)
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props,new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pass);
}
});
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(sub);
message.setText(msg);
Transport.send(message);
JOptionPane.showMessageDialog(null,"Email sended!");
} catch (MessagingException e)
{
JOptionPane.showMessageDialog(null,"Something happened!");
throw new RuntimeException(e);
}
No comments:
Post a Comment