Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cl.group2.util;
- import javax.mail.*;
- import javax.mail.Authenticator;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.Properties;
- public class Mail {
- private String mail;
- private Session session;
- Mail(String mail, String password) {
- this.mail = mail;
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "587");
- session = Session.getDefaultInstance(props, new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(mail, password);
- }
- });
- }
- public boolean sendMail(String to, String subject, String body) {
- try {
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(mail));
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- message.setSubject(subject);
- message.setText(body);
- Transport.send(message);
- return true;
- } catch (MessagingException e) {
- e.printStackTrace();
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement