Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lapr.project.controller;
- import javax.mail.*;
- import javax.mail.internet.*;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.*;
- import javax.mail.Message.RecipientType;
- public class SendEmailController {
- /**
- *
- * @param props
- * @param top
- * @param hours
- */
- public static void send(final Properties props, String top, int hours) {
- String from = props.getProperty("mail.user");
- String to = top;
- String subject = props.getProperty("testmail.subject");
- String body = props.getProperty("testmail.body");
- Session mailSession = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(props.getProperty("mail.user"), props.getProperty("mail.password"));
- }
- });
- Message simpleMessage = new MimeMessage(mailSession);
- InternetAddress fromAddress = null;
- InternetAddress toAddress = null;
- try {
- fromAddress = new InternetAddress(from);
- toAddress = new InternetAddress(to);
- } catch (AddressException e) {
- System.err.println("Error sending mail 1");
- }
- try {
- simpleMessage.setFrom(fromAddress);
- System.out.println("ola 1");
- simpleMessage.setRecipient(RecipientType.TO, toAddress);
- System.out.println("ola 2");
- simpleMessage.setSubject(subject);
- System.out.println("ola 3");
- simpleMessage.setText(body);
- System.out.println("ola 4");
- } catch (MessagingException e) {
- System.err.println("Error");
- }
- try {
- Transport.send(simpleMessage);
- System.out.println("ola 5");
- } catch (MessagingException e) {
- System.err.println("Error sending mail");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement