课程表

Struts2 教程

Struts2 标签

Struts2 集成

工具箱
速查手册

Struts2 发送电子邮件

当前位置:免费教程 » Java相关 » Struts2

本章内容将教你如何使用Struts2 应用程序发送电子邮件。学习前,你需要从JavaMail API 1.4.4下载并安装mail.jar,并将mail.jar文件放在WEB-INF\lib文件夹中,然后继续按照以下标准步骤创建action,视图和配置文件。

创建Action

首先是创建一个Action方法来处理电子邮件发送。让我们创建一个包含以下内容的名为Emailer.java的新类:

  1. package com.w3xue.struts2;
  2.  
  3. import java.util.Properties;
  4. import javax.mail.Message;
  5. import javax.mail.PasswordAuthentication;
  6. import javax.mail.Session;
  7. import javax.mail.Transport;
  8. import javax.mail.internet.InternetAddress;
  9. import javax.mail.internet.MimeMessage;
  10.  
  11. import com.opensymphony.xwork2.ActionSupport;
  12.  
  13. public class Emailer extends ActionSupport {
  14.  
  15. private String from;
  16. private String password;
  17. private String to;
  18. private String subject;
  19. private String body;
  20.  
  21. static Properties properties = new Properties();
  22. static
  23. {
  24. properties.put("mail.smtp.host", "smtp.gmail.com");
  25. properties.put("mail.smtp.socketFactory.port", "465");
  26. properties.put("mail.smtp.socketFactory.class",
  27. "javax.net.ssl.SSLSocketFactory");
  28. properties.put("mail.smtp.auth", "true");
  29. properties.put("mail.smtp.port", "465");
  30. }
  31.  
  32. public String execute()
  33. {
  34. String ret = SUCCESS;
  35. try
  36. {
  37. Session session = Session.getDefaultInstance(properties,
  38. new javax.mail.Authenticator() {
  39. protected PasswordAuthentication
  40. getPasswordAuthentication() {
  41. return new
  42. PasswordAuthentication(from, password);
  43. }});
  44.  
  45. Message message = new MimeMessage(session);
  46. message.setFrom(new InternetAddress(from));
  47. message.setRecipients(Message.RecipientType.TO,
  48. InternetAddress.parse(to));
  49. message.setSubject(subject);
  50. message.setText(body);
  51. Transport.send(message);
  52. }
  53. catch(Exception e)
  54. {
  55. ret = ERROR;
  56. e.printStackTrace();
  57. }
  58. return ret;
  59. }
  60.  
  61. public String getFrom() {
  62. return from;
  63. }
  64.  
  65. public void setFrom(String from) {
  66. this.from = from;
  67. }
  68.  
  69. public String getPassword() {
  70. return password;
  71. }
  72.  
  73. public void setPassword(String password) {
  74. this.password = password;
  75. }
  76.  
  77. public String getTo() {
  78. return to;
  79. }
  80.  
  81. public void setTo(String to) {
  82. this.to = to;
  83. }
  84.  
  85. public String getSubject() {
  86. return subject;
  87. }
  88.  
  89. public void setSubject(String subject) {
  90. this.subject = subject;
  91. }
  92.  
  93. public String getBody() {
  94. return body;
  95. }
  96.  
  97. public void setBody(String body) {
  98. this.body = body;
  99. }
  100.  
  101. public static Properties getProperties() {
  102. return properties;
  103. }
  104.  
  105. public static void setProperties(Properties properties) {
  106. Emailer.properties = properties;
  107. }
  108. }

如上面的源代码所示,Emailer.java具有与下面给出的email.jsp页面中的表单属性相对应的属,这些属性分别是:

  • from - 发件人的电子邮件地址。由于我们使用Google的SMTP,因此我们需要有效的gtalk ID。

  • password - 上述帐户的密码

  • to - 发送电子邮件给谁?

  • Subject - 电子邮件的主题

  • body - 实际的电子邮件内容

我们没有考虑对上述字段的任何验证,验证将在下一章添加。让我们看看execute()方法, execute()方法使用javax Mail库提供的参数发送电子邮件。如果邮件成功发送,action返回SUCCESS,否则返回ERROR。

创建主页

现行编写主页的JSP文件index.jsp,这将用于收集上面提到的电子邮件相关信息:

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5. "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <title>Email Form</title>
  9. </head>
  10. <body>
  11. <em>The form below uses Google's SMTP server.
  12. So you need to enter a gmail username and password
  13. </em>
  14. <form action="emailer" method="post">
  15. <label for="from">From</label><br/>
  16. <input type="text" name="from"/><br/>
  17. <label for="password">Password</label><br/>
  18. <input type="password" name="password"/><br/>
  19. <label for="to">To</label><br/>
  20. <input type="text" name="to"/><br/>
  21. <label for="subject">Subject</label><br/>
  22. <input type="text" name="subject"/><br/>
  23. <label for="body">Body</label><br/>
  24. <input type="text" name="body"/><br/>
  25. <input type="submit" value="Send Email"/>
  26. </form>
  27. </body>
  28. </html>

创建视图

现在创建success.jsp文件,这在action返回SUCCESS结果的情况下会被调用,但如果从action返回ERROR结果,我们将用另一个视图文件。

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5. "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <title>Email Success</title>
  9. </head>
  10. <body>
  11. Your email to <s:property value="to"/> was sent successfully.
  12. </body>
  13. </html>

以下将是在action返回ERROR的情况下调用的视图文件error.jsp

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5. "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <title>Email Error</title>
  9. </head>
  10. <body>
  11. There is a problem sending your email to <s:property value="to"/>.
  12. </body>
  13. </html>

配置文件

最后,让我们使用struts.xml配置文件将所有内容放在一起,如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5.  
  6. <struts>
  7.  
  8. <constant name="struts.devMode" value="true" />
  9. <package name="helloworld" extends="struts-default">
  10.  
  11. <action name="emailer"
  12. class="com.w3xue.struts2.Emailer"
  13. method="execute">
  14. <result name="success">/success.jsp</result>
  15. <result name="error">/error.jsp</result>
  16. </action>
  17.  
  18. </package>
  19.  
  20. </struts>

以下是web.xml文件的内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  7. id="WebApp_ID" version="3.0">
  8. <display-name>Struts 2</display-name>
  9. <welcome-file-list>
  10. <welcome-file>index.jsp</welcome-file>
  11. </welcome-file-list>
  12.  
  13. <filter>
  14. <filter-name>struts2</filter-name>
  15. <filter-class>
  16. org.apache.struts2.dispatcher.FilterDispatcher
  17. </filter-class>
  18. </filter>
  19.  
  20. <filter-mapping>
  21. <filter-name>struts2</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24. </web-app>

现在,右键单击项目名称,然后单击“Export”> “WAR File”以创建WAR文件。然后在Tomcat的webapps目录中部署WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp,将显示以下界面:

电子邮件用户输入

输入所需的信息,然后单击Send Email按钮。如果一切正常,那么你可以看到以下页面:

电子邮件成功

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号