Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void sendMailViaGmail(final String email, final String password, String sendTo, String subject, String body) {
- java.util.Properties properties = new java.util.Properties();
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.starttls.enable", "true");
- properties.put("mail.smtp.host", "smtp.gmail.com");
- properties.put("mail.smtp.port", "587");
- javax.mail.Session session = javax.mail.Session.getInstance(properties, new javax.mail.Authenticator() {
- protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
- return new javax.mail.PasswordAuthentication(email, password);
- }
- });
- try {
- javax.mail.Message message = new javax.mail.internet.MimeMessage(session);
- message.setFrom(new javax.mail.internet.InternetAddress(email));
- message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(sendTo));
- message.setSubject(subject);
- message.setText(body);
- javax.mail.Transport.send(message);
- } catch (Exception e) {
- System.out.println(e);
- }
- }
Add Comment
Please, Sign In to add comment