Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.Properties;
- import javax.mail.*;
- import javax.mail.internet.*;
- public class EmailSenderFromFile {
- public static void main(String[] args) throws IOException {
- Scanner scan = new Scanner(System.in);
- Path fileName = Path.of("D:/JavaBasicBabyBeginner/NestedConditionalStatment/Java-Fundamentals-May-2020/11 Arrays Exercise/src/emailText2.txt"); //file with e-mail address
- Files.readString(fileName);
- String actual = Files.readString(fileName);
- final String password = "**********"; // password here
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp-mail.outlook.com");
- props.put("mail.smtp.port", "587");
- Session session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- session.setDebug(true);
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(username));
- message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(actual)); // like [email protected]
- message.setSubject("Ping Rado");
- message.setText("Hello, this is example of sending Java email ");
- Transport.send(message);
- System.out.println("Done");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- }
Add Comment
Please, Sign In to add comment