Guest User

Untitled

a guest
Feb 19th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. package javamail;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12.  
  13. public class JavaMailTest {
  14. public static void main(String[] args) {
  15. String host="host";
  16. final String user="username@domain.com";//change accordingly
  17. String to="username@domain.com";//change accordingly
  18.  
  19. //Get the session object
  20. Properties props = new Properties();
  21. props.put("mail.smtp.host",host);
  22. props.put("mail.smtp.auth", "false");
  23.  
  24. Session session=Session.getDefaultInstance(props, null);
  25. session.setDebug(true);
  26.  
  27. //Compose the message
  28. try {
  29. MimeMessage message = new MimeMessage(session);
  30. message.saveChanges();
  31. message.setFrom(new InternetAddress(user));
  32. message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  33. message.setSubject("Test mail");
  34. message.setText("This is test mail.");
  35.  
  36. //send the message
  37. Transport.send(message);
  38.  
  39. System.out.println("message sent successfully...");
  40. }
  41. catch (MessagingException e) {e.printStackTrace();}
  42.  
  43. }
  44. }
  45.  
  46. Transport.send(msg);
  47. Folder sent = store.getFolder("Sent");
  48. sent.appendMessages(new Message[] { msg });
  49.  
  50. com.sun.mail.util.MailConnectException:
  51.  
  52. Couldn't connect to host, port: host, 25; timeout -1;
  53. nested exception is: java.net.UnknownHostException: host
  54.  
  55. String host = "outlook.office365.com";
  56. Properties props = new Properties();
  57. props.put("mail.smtp.auth", "true");
  58. props.put("mail.smtp.starttls.enable", "true");
  59. props.put("mail.smtp.host", host); // mail server host
  60. props.put("mail.smtp.port", "587"); // port
  61.  
  62. Store store = session.getStore("imaps");
  63. store.connect("imap-mail.outlook.com", "username", "password");
  64. Folder folder = store.getFolder("Sent Items");
  65. folder.open(Folder.READ_WRITE);
  66. message.setFlag(Flag.SEEN, true);
  67. folder.appendMessages(new Message[] {message});
  68. store.close();
Add Comment
Please, Sign In to add comment