I am creating an app for email sending in java, everything is set and working properly. but the error is when I try to login in Hotmail account it shows error of incorrect email and password which I have given in catch block.
but It logins in hotmail when I first login in yahoo or gmail, after that I can login in Hotmail, but I can not login in hotmail first, Why?! Code is below
private void cmd_loginActionPerformed(java.awt.event.ActionEvent evt) {
user = txt_user.getText();
pass = txt_pass.getText();
int combo = combo_ac.getSelectedIndex();
if(combo==0){
try{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session sessin = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user+"@gmail.com", pass);
}
}
);
new Gmail().setVisible(true);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Incorrect Email or Password!");
}
}else if(combo==1){
try{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.socketFactory.port", "25");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
Session sessin = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user+"@hotmail.com", pass);
}
}
);
new Hotmail().setVisible(true);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Incorrect Email or Password!");
}
}else if(combo==2){
try{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session sessin = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user+"@yahoo.com", pass);
}
}
);
new Yahoo().setVisible(true);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Incorrect Email or Password!");
}
}
}
This article would explain why apparently you can log in into your hotmail account after log in into gmail or yahoo. Actually you're logging in into the same account you just did at first try. The thing is you're using
Session#getDefaultInstance()
insteadSession#getInstance()
:However it doesn't explain why you can't log in into your hotmail account but you should post the exception stack trace so we can help you.
Update:
Based on this stack trace:
This means you have to set
mail.smtp.starttls.enable
property ontrue
as per Hotmail requirement:Note: Extracted from Configure Outlook to connect to an MSN email account.
You should only add the following line to your code when trying to log in into your Hotmail account:
Also don't forget replace
Session.getDefaultInstance
bySession.getInstance
as I suggested above.