Thursday, August 2, 2012

send email on behave of your outlook account


                String to = "test@gmail.com";
String from = "dwei@companyname.com";
Properties properties = System.getProperties();

properties.setProperty("mail.transport.protocol","smtp");
properties.setProperty("mail.smtp.host", "mail.mpk.autc.com");
properties.setProperty("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(properties, auth);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(msg);
message.setText(msg);
Transport.send(message);
}catch (Exception e) {
e.printStackTrace();
}




class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
           String username = "myOutlookUserName";
           String password = "myPassword";
           return new PasswordAuthentication(username, password);
        }
    }

No comments:

Post a Comment