[英]Java sending a mail via tomcat server
我正在嘗試通過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秒。
謝謝
您的android應用應在單獨的線程中發送請求,因為通過主線程發送請求會阻塞應用的GUI。 缺省情況下,Tomcat服務器處理不同線程中的請求。 因此,您無需在請求處理內部啟動單獨的線程。 您可以像這樣啟動一個新線程(請參閱java.lang.Thread
文檔):
Thread theThread = new Thread(new Runnable() {
@Override
public void run() {
// send the request to the tomcat server
}
});
theThread.start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.