Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javamail;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class JavaMailTest {
- public static void main(String[] args) {
- String host="host";
- final String user="username@domain.com";//change accordingly
- String to="username@domain.com";//change accordingly
- //Get the session object
- Properties props = new Properties();
- props.put("mail.smtp.host",host);
- props.put("mail.smtp.auth", "false");
- Session session=Session.getDefaultInstance(props, null);
- session.setDebug(true);
- //Compose the message
- try {
- MimeMessage message = new MimeMessage(session);
- message.saveChanges();
- message.setFrom(new InternetAddress(user));
- message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
- message.setSubject("Test mail");
- message.setText("This is test mail.");
- //send the message
- Transport.send(message);
- System.out.println("message sent successfully...");
- }
- catch (MessagingException e) {e.printStackTrace();}
- }
- }
- Transport.send(msg);
- Folder sent = store.getFolder("Sent");
- sent.appendMessages(new Message[] { msg });
- com.sun.mail.util.MailConnectException:
- Couldn't connect to host, port: host, 25; timeout -1;
- nested exception is: java.net.UnknownHostException: host
- String host = "outlook.office365.com";
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", host); // mail server host
- props.put("mail.smtp.port", "587"); // port
- Store store = session.getStore("imaps");
- store.connect("imap-mail.outlook.com", "username", "password");
- Folder folder = store.getFolder("Sent Items");
- folder.open(Folder.READ_WRITE);
- message.setFlag(Flag.SEEN, true);
- folder.appendMessages(new Message[] {message});
- store.close();
Add Comment
Please, Sign In to add comment