この章では、Struts2のアプリケーションを使用してメールを送信する方法を教えてください。この演習では、javamail API 1.4.4からmail.jarをダウンロードしてインストールし、mail.jarファイルをWeb-inflibフォルダーに配置し、標準の手順に従ってアクション、ビュー、および構成ファイルを作成し続ける必要があります。
アクションを作成する:
次のステップは、アクションメソッドを作成してメールを送信することです。次のコンテンツを使用して、Emailer.javaという新しいクラスを作成しましょう。
パッケージ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.intertaddress.mail com.opensymphony.xwork2.actionsupport; public class Emailerはactionsupportを拡張します{private string from;プライベート文字列パスワード。プライベートストリングに;プライベート文字列の件名;プライベートストリングボディ;静的プロパティプロパティ= 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);}});メッセージメッセージ= new mimemessage(session); message.setfrom(new InternetAddress(from)); message.setRecipients(message.RecipientType.to、InternetAddress.Parse(to)); message.setsubject(subject); message.settext(body); Transport.send(メッセージ); } catch(例外e){ret = error; e.printstacktrace(); } rett; } public string getFrom(){return; } public void setfrom(string from){this.from = from; } public string getPassWord(){パスワードを返します。 } 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プロパティgetProperties(){return Properties; } public static void setProperties(プロパティプロパティ){emailer.properties = properties; }}上記のソースコードでは、emailer.javaには、以下のフォームに示されているemail.jspページの対応するプロパティがあることがわかります。これらのプロパティ
上記の属性のそれぞれの検証を検討しましたか?次の章で検証を追加します。次に、execute()メソッドを見てみましょう。 execute()メソッドは、Javax Mailライブラリを使用して、提供されたパラメーターを使用して電子メールを送信します。メッセージが送信された場合、アクションは成功を返し、それ以外の場合はエラーを返します。
ホームページを作成する:
上記のように、ホームページindex.jspのJSPファイルを書いてみましょう。
<%@ page Language = "Java" contentType = "text/html; charset = iso-8859-1" pageencoding = "iso-8859-1"%> <%@@@@@@@@@@@@ stuts-tags "uri =" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <head> <title> form </title> </head> <body> <em>以下のフォームはGoogleのSMTPサーバーを使用します。したがって、gmailユーザー名とパスワードを入力する必要があります</em> <form action = "emailer" method = "post"> <label = "from </label> <br/> <入力タイプ="テキスト "name =" from "/> <br/> for = "to"> to </label> <br/> <入力タイプ= "text" name = "to"/> <br/> <ラベル= "件名">件名</label> <br/> <入力タイプ= "テキスト" name = "subject"/> <br/> </form> </body> </html>
ビューを作成します:
JSPファイルのsuccess.jspを使用して、アクションが呼び出される場合に成功を返しますが、エラーが発生した場合、ファイルがアクションから返されるという別の見解があります。
<%@ page Language = "Java" contentType = "text/html; charset = iso-8859-1" pageencoding = "iso-8859-1"%> <%@@@@@@@@@@@@ stuts-tags "uri =" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title> email success </title> </head> <body> your email to <s:property value =" to "/>は成功しました。</body> </html> </html>
以下はエラーの場合に、アクションからビューファイルエラー.jspを返します。
<%@ page Language = "Java" contentType = "text/html; charset = iso-8859-1" pageencoding = "iso-8859-1"%> <%@@@@@@@@@@@@ stuts-tags "uri =" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>エラー</title> </head> <body> <s:s:property value =" to "/> .</ body> </html> </html>
構成ファイル:
次のように、これらすべてをstruts.xmlの構成ファイルと組み合わせてみましょう。
<?xml version = "1.0" encoding = "utf-8"?> <!doctype struts public " - // apacheソフトウェア財団// dtd struts configuration 2.0 // en" "http://struts.apache.org/dtds/struts-2.0.dtd" name = "helloworld" extends = "struts-default"> <action name = "emailer" method = "execute"> <result name = "success">/success.jsp </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/nns/javaee xmlns:web = "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"バージョン= "3.0 <welcome-file> index.jsp </welcome-file> </welcome-file-list> <filter> <filter-name> struts2 </filter-name> <filter-class> org.stache.struts2.dispatcher.filterdispatcher </filter-class> </filter> <filter-name> </フィルターマッピング> </web-app>
次に、プロジェクト名を右クリックして[エクスポート]> [WARファイル]をクリックして[WARファイル]を作成します。次に、この戦争をTomcatのWebAppsディレクトリに展開します。最後に、Tomcatサーバーを起動し、URL http:// localhost:8080/helloworldstruts2/index.jspにアクセスしてみてください。これにより、次の写真が表示されます。
必要な情報を入力し、[電子メールの送信]ボタンをクリックします。すべてがうまくいけば、次のページを表示する必要があります。
3つのSSHフレームワークが一緒に使用される場合は、ここで例を挙げますが、StrutsとSpringのフレームワーク、Mail.jar、Activation.jarも必要です。
1)ApplicationContext.xmlファイルで最初にBeanを構成します
<bean id = "mailsender"> <プロパティ名= "host" value = "host"/> <プロパティ名= "username" value = "username"/> <property name = "property name =" password "value"/> </bean> <bean id = "sendmailaction" singleton = "false"> <propationedender "
2)電子メールを送信するためにJavaクラスコードを実装します
Protected javamailsenderimpl mailsender; public class sendmailaction extends actionsupport {public void setmailsender(javamailsenderimpl mailsender){this.mailsender = mailsender;} public void sendmail()Sthrows Exception1:簡単なメール
Protected javamailsenderimpl mailsender; public class sendmailaction extends actionsupport {public void setmailsender(javamailsenderimpl mailsender){this.mailsender = mailsender;} public void sendmail()Sthrows Exception 2:HTMLメールを送信します
//電子メールメッセージを作成し、簡単な電子メールとHTML電子メールを送信しますmimemessage mailmessage = senderimpl.createmimemessage(); mimemessagehelper messagehelper = new mimemessagehelper(mailmessage); //受信者と送信者MessageHelper.setto( "[email protected]")を設定します。 messagehelper.setfrom( "[email protected]"); messagehelper.setsubject( "test html mail!"); // true message messagehelper.settext( "<html> <head> </head> <body> <h1> hello !! zhangjian </h1> </body> </html>"、true); //電子メールsenderimpl.send(mailmessage);
3:このテストメールのネストされた写真
//電子メールメッセージを作成し、簡単な電子メールとHTML電子メールを送信しますmimemessage mailmessage = senderimpl.createmimemessage(); //ここのブール値は、それが真実である場合にのみ画像をネストできることに注意してください。 mimemessagehelperを構築する場合、与えられた値は有効になります。マルチパートモードmimemessagehelper messagehelper = new mimemessagehelper(mailmessage、true); //受信者を設定し、送信者messagehelper.setto( "[email protected]"); messagehelper.setfrom( "[email protected]"); messagehelper.setsubject( "テストメールでネストされた画像!!"); // trueは、HTML形式のメッセージを示しますmessagehelper.settext( "<html> <head> </head> <body> <h1> hello !! zhangjian </h1>" + "<img src ="/"mce_src ="/"/" "cid:aaa/" // </body> </html> " filesystemResource IMG = new FilesystemResource(new File( "c:/aaa.jpg")); messagehelper.addinline( "aaa"、img); //電子メールsenderimpl.send(mailmessage); }}