程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 應用Java的Struts框架完成電子郵件發送功效

應用Java的Struts框架完成電子郵件發送功效

編輯:關於JAVA

應用Java的Struts框架完成電子郵件發送功效。本站提示廣大學習愛好者:(應用Java的Struts框架完成電子郵件發送功效)文章只能為提供參考,不一定能成為您想要的結果。以下是應用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("[email protected]"); 
   messageHelper.setFrom("[email protected]"); 
   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("[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>",true); 
      
   FileSystemResource img = new FileSystemResource(new File("c:/aaa.jpg")); 
   
   messageHelper.addInline("aaa",img); 
   
  //發送郵件 
   senderImpl.send(mailMessage); 

}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved