Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private String userpass;
- private String from;
- //attachments not required
- private boolean sendMail(String to, String user, String pass, String subject, String p_message){
- from = user;
- userpass = pass;
- String host = "localhost";
- Properties props = new Properties();
- props.setProperty("mail.smtp.ssl.enable", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "587");
- props.put("mail.imap.ssl.enable", "true"); // required for Gmail
- props.put("mail.imap.sasl.enable", "true");
- props.put("mail.imap.sasl.mechanisms", "XOAUTH2");
- props.put("mail.imap.auth.login.disable", "true");
- props.put("mail.imap.auth.plain.disable", "true");
- Session session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(from, userpass);
- }
- });
- try{
- Store store = session.getStore("imap");
- store.connect("imap.gmail.com", from, userpass);
- }
- catch (Exception e){
- e.printStackTrace();
- }
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(from));
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(to));
- message.setSubject(subject);
- message.setText(p_message);
- Transport.send(message);
- System.out.println("Sent to "+to);
- return true;
- }
- catch (javax.mail.AuthenticationFailedException auth_e){
- auth_e.printStackTrace();
- System.out.println("Invalid username or password");
- return false;
- }
- catch (MessagingException e) {
- e.printStackTrace();
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement