我正在尝试通过Android应用程序发送邮件,邮件必须通过我的服务器发送(因为Gmail的邮件API需要用户名和密码,所以,我不希望我的数据在代码本身)
但是我遇到了socketTimeOutException的错误。 我可以增加超时但是在收到响应之前我什么也做不了,它需要25秒。 如果我把发送功能放在一个线程中,它就根本不发送邮件。
这是服务器的代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
JSONObject res = new JSONObject();
try {
System.out.println("Post ForgotMypass");
Mongo mongo = new Mongo(LogIn.host, 27017);
DB db = mongo.getDB("Users");
DBCollection collection = db.getCollection("usersCollection");
// get a myUser object and cheack for jobs
String json = request.getParameter("JSONObj");
JSONParser jp = new JSONParser();
JSONObject temp = (JSONObject) jp.parse(json);
String mail = (String) temp.get("mail");
if (mail != null) {
BasicDBObject searchQuer = new BasicDBObject();
searchQuer.put("Mail", mail);
DBObject Item = collection.findOne(searchQuer);
if (Item != null) {
JSONParser jp2 = new JSONParser();
JSONObject Is = (JSONObject) jp2.parse(Item.toString());
JSONObject I = (JSONObject) Is.get("_id");
String id = (String) I.get("$oid");
if (id != null) {
String Dmail = URLDecoder.decode(mail, "UTF-8");
SendMailSSL.SendMail(Dmail, 4, id);
StringWriter out = new StringWriter();
res.put("Status", "true");
res.writeJSONString(out);
response.getOutputStream().println(out.toString());
System.out.println("mail sent succesfully");
} else {
StringWriter out = new StringWriter();
res.put("Status", "falseNoMail");
System.out.println("ver false no mail");
res.writeJSONString(out);
response.getOutputStream().println(out.toString());
}
} else {
StringWriter out = new StringWriter();
res.put("Status", "ObjectdoesntExists");
System.out.println("ver ObjectdoesntExists");
res.writeJSONString(out);
response.getOutputStream().println(out.toString());
}
} else {// id is null
StringWriter out = new StringWriter();
res.put("Status", "falseIdNUll");
System.out.println("ver falseIdNUll");
res.writeJSONString(out);
response.getOutputStream().println(out.toString());
}
} catch (Exception e) {
e.printStackTrace();
StringWriter out = new StringWriter();
res.put("Status", "false");
System.out.println("ver false");
res.writeJSONString(out);
response.getOutputStream().println(out.toString());
}
}
和:
public static void SendMail(String to, int typeOfMessage, String UserID) {
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 session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("adress"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
/*
* all the type of messages
*/
if (typeOfMessage == 1) {
message.setSubject("title");
message.setText("text");
}else if (typeOfMessage == 2){
message.setSubject("title");
message.setText("text");
}else if (typeOfMessage == 3){
message.setSubject("title");
message.setText("text");
}else if (typeOfMessage == 4){
message.setSubject("title");
message.setText("text");
}
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
所以,有人知道如何避免这个问题。 更具体一点,通过服务器发送邮件,但之后,我以某种方式将响应发送到Android客户端,所以他不必等待25秒。
感谢
答案 0 :(得分:0)
你的Android应用程序应该在一个单独的线程中发送请求,因为通过主线程发送它将阻止你的应用程序的GUI。默认情况下,Tomcat-Server处理不同线程中的请求。因此,您无需在请求处理中启动单独的线程。你可以开始这样的新线程(参见java.lang.Thread
的文档):
Thread theThread = new Thread(new Runnable() {
@Override
public void run() {
// send the request to the tomcat server
}
});
theThread.start();