Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
package beans;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.FlagTerm;
public class ReplyMail {
static String host = "imap.gmail.com";
static String user = "riteshdiwakar87";
static String password = "xyz";
static String to="
[email protected]";
public static void main(String args[]) throws MessagingException{
try{
Properties props=new Properties();
Session session=Session.getDefaultInstance(props,null);
Store store=session.getStore("imaps");
store.connect(host, user, password);
System.out.print(store.isConnected());
//=====================================================================================================================
Folder fldr=store.getFolder("inbox");
fldr.open(Folder.READ_ONLY);
Message[] msg=fldr.getMessages();
MimeMessage msgs=null;
String sender;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<msg.length;i++){
System.out.println("subject====="+msg[i].getSubject());
System.out.print("Do you want to reply [y/n] : ");
String ans = reader.readLine();
if(ans.equals("y")||ans.equals("Y")){
msgs=(MimeMessage)msg[i].reply(true);
System.out.println("msg subject="+msgs.getSubject().toString());
sender=msg[i].getAllRecipients()[0].toString();
System.out.println("msg sender=:"+sender);
msgs.setSender(new InternetAddress(sender));
System.out.println(msgs.getSender());
String content="<html><head></head><body>kjdfgkdfgkdgkdjgkjdfng"+msg[i].getContent()+"</body></html>";
msgs.setContent(content,"text/html");
System.out.println("content "+msgs.getContent());
msgs.addRecipient(Message.RecipientType.TO, new InternetAddress(sender));
break;
}
if(ans.equals("n")||ans.equals("N")){
continue;
}
}
store.close();
Properties prps=System.getProperties();
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
prps.put("mail.smtp.socketFactory.port", "465");
prps.put("mail.smtp.socketFactory.class",SSL_FACTORY);
prps.put("mail.smtp.socketFactory.fallback", "false");
prps.setProperty("mail.transport.protocol", "smtp");
prps.setProperty("mail.smtp.host", "smtp.gmail.com");
//prps.put("mail.smtp.auth", "true");
//prps.put("mail.smtp.port", "465");
//prps.put("mail.debug", "true");
session = Session.getInstance(prps,new javax.mail.Authenticator() {
String pass="xyz";
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("riteshdiwakar87",pass);
}
});
session.setDebug(true);
System.out.println("ritu");
Transport transport = session.getTransport();
System.out.println("ritu");
transport.connect();
System.out.println(transport.isConnected());
System.out.println(msgs.getContentType());
System.out.println(msgs.getSentDate());
//Transport.send(msgs);
transport.send(msgs);
System.out.println("ritu");
//transport.close();
//=================================================================================================================================
}
catch(Exception ee){
System.out.println("exception "+ee);
}
}
}
this is my application .
i am facing an exception which is
"javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1".....
send me solution of this .
ritesh
thank you
View Answers
Related Tutorials/Questions & Answers: