利用Java的Struts框架实现电子邮件发送功能
本章将教你如何使用struts2 的应用程序发送电子邮件。对于此练习中,需要下载并安装将mail.jar 从 javamail api1.4.4,并将 mail.jar文件放置在web-inflib文件夹,然后继续遵循的标准步骤创建动作,视图和配置文件。
创建动作:
下一步是创建一个action方法,发送电子邮件。让我们创建一个新类称为 emailer.java 以下内容。
package com.yiibai.struts2; import java.util.properties; import javax.mail.message; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import com.opensymphony.xwork2.actionsupport; public class emailer extends actionsupport { private string from; private string password; private string to; private string subject; private string body; static properties properties = new properties(); static { properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.socketfactory.port", "465"); properties.put("mail.smtp.socketfactory.class", "javax.net.ssl.sslsocketfactory"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.port", "465"); } public string execute() { string ret = success; try { session session = session.getdefaultinstance(properties, new javax.mail.authenticator() { protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(from, password); }}); message message = new mimemessage(session); message.setfrom(new internetaddress(from)); message.setrecipients(message.recipienttype.to, internetaddress.parse(to)); message.setsubject(subject); message.settext(body); transport.send(message); } catch(exception e) { ret = error; e.printstacktrace(); } return ret; } public string getfrom() { return from; } public void setfrom(string from) { this.from = from; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getto() { return to; } public void setto(string to) { this.to = to; } public string getsubject() { return subject; } public void setsubject(string subject) { this.subject = subject; } public string getbody() { return body; } public void setbody(string body) { this.body = body; } public static properties getproperties() { return properties; } public static void setproperties(properties properties) { emailer.properties = properties; } }
可以看到在上面的源代码中,emailer.java有对应的形式在下面给出的email.jsp页的属性的属性。这些属性
- from - 发件人的电子邮件地址。由于我们使用的是谷歌的smtp,我们需要一个有效的gtalk id
- password - 上述帐户的密码
- to - 给谁发送电子邮件?
- subject - 电子邮件主题
- body - 实际的电子邮件消息
我们有没有考虑过上述各个属性的任何验证,验证将被添加在下一章。现在让我们看看在execute()方法。 execute()方法使用使用javax邮件库发送一封电子邮件,使用提供的参数。如果邮件被发送,动作返回 success,否则它返回error。
创建主页页:
让我们编写主页index.jsp的jsp文件,这将被用来收集电子邮件的相关信息,上面提到的:
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>email form</title> </head> <body> <em>the form below uses google's smtp server. so you need to enter a gmail username and password </em> <form action="emailer" method="post"> <label for="from">from</label><br/> <input type="text" name="from"/><br/> <label for="password">password</label><br/> <input type="password" name="password"/><br/> <label for="to">to</label><br/> <input type="text" name="to"/><br/> <label for="subject">subject</label><br/> <input type="text" name="subject"/><br/> <label for="body">body</label><br/> <input type="text" name="body"/><br/> <input type="submit" value="send email"/> </form> </body> </html>
创建视图:
我们将使用jsp文件的success.jsp将被调用的情况下行动返回success,但在发生error 的情况下,我们将有另一种视图认为文件是从操作返回。
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>email success</title> </head> <body> your email to <s:property value="to"/> was sent successfully. </body> </html>
下面将是在一个错误的情况下,从动作返回视图文件error.jsp。
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>email error</title> </head> <body> there is a problem sending your email to <s:property value="to"/>. </body> </html>
配置文件:
现在,让我们将这一切组合起来使用struts.xml的配置文件如下:
<?xml version="1.0" encoding="utf-8"?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devmode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="emailer" class="com.yiibai.struts2.emailer" method="execute"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
以下是web.xml文件中的内容:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filterdispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
现在,右键点击项目名称,并单击 export > war file创建一个war文件。然后部署此war在tomcat的webapps目录下。最后,启动tomcat服务器和尝试访问url http://localhost:8080/helloworldstruts2/index.jsp。这会给出以下画面:
输入所需信息,并单击“send email ”按钮。如果一切顺利,那么应该看到以下页面:
如果是ssh三个框架配合起来使用,这里再给个例子,不过struts和spring的框架外,也需要mail.jar,activation.jar.
1) 首先在applicationcontext.xml文件中配置bean
<bean id="mailsender" class="org.springframework.mail.javamail.javamailsenderimpl"> <property name="host" value="host"/> <property name="username" value="username"/> <property name="password" value="password"/> </bean> <bean id="sendmailaction" class="cn.com.action.sendmailaction" singleton="false"> <property name="mailsender"><ref bean="mailsender"/> </property> </bean>
2)实现发送邮件java类代码
protected javamailsenderimpl mailsender; public class sendmailaction extends actionsupport{ public void setmailsender(javamailsenderimpl mailsender) { this.mailsender = mailsender; } public void sendmail() throws exception {
1:简单邮件
protected javamailsenderimpl mailsender; public class sendmailaction extends actionsupport{ public void setmailsender(javamailsenderimpl mailsender) { this.mailsender = mailsender; } public void sendmail() throws exception {
2:html邮件的发送
//建立邮件消息,发送简单邮件和html邮件的区别 mimemessage mailmessage = senderimpl.createmimemessage(); mimemessagehelper messagehelper = new mimemessagehelper(mailmessage); //设置收件人,寄件人 messagehelper.setto("sun111@163.com"); messagehelper.setfrom("webadmin@163.com"); messagehelper.setsubject("测试html邮件!"); //true 表示启动html格式的邮件 messagehelper.settext("<html><head></head><body><h1>hello!!zhangjian</h1></body></html>",true); //发送邮件 senderimpl.send(mailmessage);
3:本类测试邮件中嵌套图片
//建立邮件消息,发送简单邮件和html邮件的区别 mimemessage mailmessage = senderimpl.createmimemessage(); //注意这里的boolean,等于真的时候才能嵌套图片,在构建mimemessagehelper时候,所给定的值是true表示启用, multipart模式 mimemessagehelper messagehelper = new mimemessagehelper(mailmessage,true); //设置收件人,寄件人 messagehelper.setto("sun111@163.com"); messagehelper.setfrom("webadmin@163.com"); messagehelper.setsubject("测试邮件中嵌套图片!!"); //true 表示启动html格式的邮件 messagehelper.settext("<html><head></head><body><h1>hello!!zhangjian</h1>" + "<img src="/" mce_src="/""cid:aaa/"/></body></html>",true); filesystemresource img = new filesystemresource(new file("c:/aaa.jpg")); messagehelper.addinline("aaa",img); //发送邮件 senderimpl.send(mailmessage); } }
上一篇: java 串口通信详细及简单实例
下一篇: jdbc连接oracle数据库功能示例