Java Mail Authentication failed Exception
843830Mar 28 2007 — edited Mar 28 2007Hi,
I used javamail api to send and receive mails from
MS Exchange server.
Send Mail Program :
package com;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Send {
public static void main (String args[]) {
try{
// Put the name of the mail
server
String host="exchange
server name";
// From address
String from="from
addres";
// To address
String to="to
address";
// Get system properties
Properties props =
System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Create mail session
Session session =
Session.getDefaultInstance(props,null);
session.setDebug(false);
// Create a message
Message mimeMsg = new
MimeMessage(session);
// Set from Address
Address addressFrom =
new InternetAddress(from);
mimeMsg.setFrom(addressFrom);
// Set to Address
Address addressTo =
new InternetAddress(to);
mimeMsg.addRecipient(Message.RecipientType.TO, addressTo);
//Set subject and message of the
mail
mimeMsg.setSubject("Testing");
mimeMsg.setText("Testing for the Application");
MimeBodyPart
mimebodypart = new MimeBodyPart();
// attach message BODY
MimeMultipart
mimemultipart = new MimeMultipart();
mimemultipart.addBodyPart(mimebodypart);
// Send the mail
Transport.send(mimeMsg);
System.out.println("Mail Sent");
}
}
It works fine.
Fetch Mail Program :
package com;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
public class Receive {
public static void main(String args[]) throws Exception
{
// Put the name of the mail server
String host = "mail server";
// Get system properties,
Properties props = System.getProperties();
props.put("mail.imap.host", host);
// Setup authentication, get session
Session session = Session.getInstance(new
Properties());
// Get the store, put pop3 if the mail server is pop3
Store store = session.getStore("imap");
// Enter the username and password while connecting
to the store
store.connect(host, "username", "password");
// Get INBOX folder
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
// Get directory
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ":" +
message.getFrom()[0]+ "\t" + message[i].getSubject() + "\t" + message[i].getFolder()+
"\t" + message[i].getContent());
System.out.println("Mail Received Successfully");
}
folder.close(false);
store.close();
}
}
It throws following error:
ERROR MESSAGE :
--------------
Exception in thread "main" javax.mail.AuthenticationFailedException:
A required privilege is not held by the client.
at
com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:436)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at com.Receive.main(Receive.java:46)
It worked fine for the past 1.5 months,suddenly throws this error.
Please let me know whther there is any issue with the mail server or
need to handle it from javamail api/fix it.
Thanks for your time,
Saravana