我使用JSP頁面來作爲電子郵件模板從我的動作類發送重定向jsp的從動作發送值未從動作
是aaa.jsp
接受來自用戶EMAIL_ID然後重定向到發送hashcode
郵件操作然後將用戶重定向到yyy.jsp
以輸入發送給用戶提供的emil_id
的hashcode
。我使用xxx.jsp
作爲電子郵件模板。
我xxx.jsp
是
<html>
<head>
</head>
<body>
<s:property value="hashcode"/>
</body>
</html>
我有一個動作類,將電子郵件發送到用戶與註冊目的的認證碼。
checkAndMail
用於檢查此郵件是否已註冊,並轉換xxx.jsp
頁串
public String checkAndMail() throws Exception{
this.hashcode="9048075352";//getters and setters are used for hashxode and is of type String
/***********
code for searching whether the email is registered already
************/
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response) {
private final StringWriter sw = new StringWriter();
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(sw);
}
@Override
public String toString() {
return sw.toString();
}
};
setHashcode(hashcode);
request.getRequestDispatcher("aa/bb/xxx.jsp").include(request,
responseWrapper);
String result1 = responseWrapper.toString();
sendMailNew("[email protected]", result1,"AssignMail");
TARGET = "success";
}
我打電話sendMailNew
從checkAndMail
用於發送郵件。我sendMailNew
其用於發送郵件的方法是
public synchronized void sendMailNew(String mailTo,String Content,String type) throws Exception
{
String TEXT="";
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.user", USER);
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
//message.setText(TEXT);
if(type.equals("AssignMail"))
{
message.setContent(Content, "text/html");
}
message.setSubject(SUBJECT);
message.setFrom(new InternetAddress(FROM));
message.addRecipients(RecipientType.TO, mailTo);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
throw new Exception("Exception in Mail.sendMail" + e.getMessage());
}
}
我的問題是如何傳遞的值(這裏參數爲hashcode
)從checkAdnMail
到xxx.jsp
這個動作類被重定向到yyy.jsp
爲進入hashcode
應該發送到用戶的電子郵件地址。
參考:Struts2 - How can I get the result of a JSP page as a string in an action class (for emails)
請參閱http://stackoverflow.com/q/13623220/1700321。 – 2014-10-01 12:16:39