Hello, I'm trying to use Exchange authentication from my app using JavaMail to do this. Could some one give me a guide to do this? After authentication I need to send mails that's the main reason that I'm using JavaMail. All the links that I found talks about problems with this but I think this must be an easy task to do from Java. Thanks in advance.
A:
Works for me:
Properties props = System.getProperties();
// Session configuration is done using properties. In this case, the IMAP port. All the rest are using defaults
props.setProperty("mail.imap.port", "993");
// creating the session to the mail server
Session session = Session.getInstance(props, null);
// Store is JavaMails name for the entity holding the mails
Store store = session.getStore("imaps");
// accessing the mail server using the domain user and password
store.connect(host, user, password);
// retrieving the inbox folder
Folder inbox = store.getFolder("INBOX");
This code is based on the sample code arrives with the download of java mail.
David Rabinowitz
2009-11-11 11:43:06
Thanks for your answer.I don't understand your code could you comment your code or something?
rafaelochoa
2009-11-16 20:26:22
Added comments.
David Rabinowitz
2009-11-16 23:49:09
A:
After authentication I need to send mails
The below example works fine here with Exchange servers:
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "mail.example.com");
properties.put("mail.smtp.port", "2525");
properties.put("mail.smtp.auth", "true");
final String username = "username";
final String password = "password";
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
Transport transport = null;
try {
Session session = Session.getDefaultInstance(properties, authenticator);
MimeMessage mimeMessage = createMimeMessage(session, mimeMessageData);
transport = session.getTransport();
transport.connect(username, password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
} finally {
if (transport != null) try { transport.close(); } catch (MessagingException logOrIgnore) {}
}
BalusC
2009-11-11 12:30:27
Thanks for your answer.I'm using the same username and password with my outlook and is working.Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: XXX.XXX.XXX.XXX, port: 2525; nested exception: java.net.ConnectException: Connection timed out: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1545) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453) at javax.mail.Service.connect(Service.java:291) at javax.mail.Service.connect(Service.java:172) at javax.mail.Service.connect(Service.java:192)any idea?
rafaelochoa
2009-11-16 20:25:21
You need to change the port number to the actual port number of the server. It's usually 25, but it can be different. Consult mailserver's admin for details. Sorry for not mentioning that in the message, I thought it'd be obvious enough.
BalusC
2009-11-16 21:03:13
A:
Exchange does not start SMTP service by default, so we can't use SMTP protocol to connect to Exchange server and try to send email. BalusC can work fine with the above code because your mailserver administrator enabled SMTP service on Exchange while in most case SMTP is disabled. i am also looking for solution. http://www.moyosoft.com/jbex/?gclid=CMGLup-E4KICFQ8aewodhWSiRQ is the best answer among what i have found, but what a frustration you should pay for it after 60 days.
tenebaul
2010-07-10 08:12:24