grach

MailJavaSeder

Jul 19th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.util.Properties;
  6. import javax.mail.*;
  7. import javax.mail.internet.*;
  8.  
  9. public class EmailSenderFromFile {
  10.     public static void main(String[] args) throws IOException {
  11.         Scanner scan = new Scanner(System.in);
  12.  
  13.         Path fileName = Path.of("D:/JavaBasicBabyBeginner/NestedConditionalStatment/Java-Fundamentals-May-2020/11 Arrays Exercise/src/emailText2.txt"); //file with e-mail address
  14.  
  15.         Files.readString(fileName);
  16.  
  17.  
  18.         String actual = Files.readString(fileName);
  19.  
  20.  
  21.  
  22.  
  23.         final String username = "[email protected]";  // like [email protected]
  24.         final String password = "**********";   // password here
  25.  
  26.         Properties props = new Properties();
  27.         props.put("mail.smtp.auth", "true");
  28.         props.put("mail.smtp.starttls.enable", "true");
  29.         props.put("mail.smtp.host", "smtp-mail.outlook.com");
  30.        
  31.         props.put("mail.smtp.port", "587");
  32.  
  33.         Session session = Session.getInstance(props,
  34.                 new javax.mail.Authenticator() {
  35.                     @Override
  36.                     protected PasswordAuthentication getPasswordAuthentication() {
  37.                         return new PasswordAuthentication(username, password);
  38.                     }
  39.                 });
  40.         session.setDebug(true);
  41.  
  42.         try {
  43.  
  44.             Message message = new MimeMessage(session);
  45.             message.setFrom(new InternetAddress(username));
  46.  
  47.  
  48.             message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(actual));   // like [email protected]
  49.             message.setSubject("Ping Rado");
  50.             message.setText("Hello, this is example of sending Java email ");
  51.  
  52.             Transport.send(message);
  53.  
  54.             System.out.println("Done");
  55.  
  56.         } catch (MessagingException e) {
  57.             throw new RuntimeException(e);
  58.         }
  59.     }
  60. }
Add Comment
Please, Sign In to add comment