Nếu lỗi không send email được thì vui lòng
Download và import 2 file dưới đây
mail.jar & activation.jar Download
Download và import 2 file dưới đây
mail.jar & activation.jar Download
Send Attachment in E-mail - Chỉ gửi được file
Send a Simple E-mail - Chỉ gửi được Message
sendemail.jsp
Java Struts 2 Framework 2016
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts2 - Send Email Attachment</title>
<style>
table{background-color: #c7e6ff ;border: 1px solid #00BCD4;}
.actionMessage{list-style-type: none; color: green;}
.errorMessage{list-style-type: none; color: red;}
</style>
</head>
<body>
<center>
<s:if test="hasActionErrors()">
<s:actionerror />
</s:if>
<s:if test="hasActionMessages()">
<s:actionmessage />
</s:if>
<s:form cssClass="bg" action="sendEmail" method="post" enctype="multipart/form-data">
<s:textfield label="To Address" name="emailToAddress"></s:textfield>
<s:textfield label="Subject" name="emailSubject"></s:textfield>
<s:textarea label="Message" name="emailBodyText" rows="5" cols="50"></s:textarea>
<s:file label="File" name="file"></s:file>
<s:submit value="Send Email"></s:submit>
</s:form>
</center>
</body>
</html>
struts.xml
Java Struts 2 Framework 2016
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="sendEmail" class="com.giaima.SendEmailAction">
<result name="success">/sendemail.jsp</result>
<result name="input">/sendemail.jsp</result>
<interceptor-ref name="defaultStack">
<param name="maximumSize">10485760</param>
<param name="allowedTypes">text/plain,image/jpeg,image/png,image/gif,image/pjpeg</param>
</interceptor-ref>
</action>
</package>
</struts>
SendEmailAction.java
Java Struts 2 Framework 2016
package com.giaima;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.io.File;
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class SendEmailAction extends ActionSupport implements
ServletContextAware {
private static final long serialVersionUID = 1L;
private String emailSubject;
private String emailBodyText;
private String emailToAddress;
private File file;
private String fileFileName;
private ServletContext servletContext;
public String getEmailSubject() {
return emailSubject;
}
public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}
public String getEmailBodyText() {
return emailBodyText;
}
public void setEmailBodyText(String emailBodyText) {
this.emailBodyText = emailBodyText;
}
public String getEmailToAddress() {
return emailToAddress;
}
public void setEmailToAddress(String emailToAddress) {
this.emailToAddress = emailToAddress;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public ServletContext getServletContext() {
return servletContext;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public String execute() throws Exception {
SendEmail.sendEmail(emailToAddress, emailSubject, emailBodyText, file, fileFileName);
addActionMessage("Email with attachment successfully sent!");
return SUCCESS;
}
@Override
public void validate() {
if (emailToAddress.length() == 0) {
String errorMsg = "You must provide To Email Address!";
addActionError(errorMsg);
}
if (emailSubject.length() == 0) {
String errorMsg = "You must provide Email Subject!";
addActionError(errorMsg);
}
if (emailBodyText.length() == 0) {
String errorMsg = "You must provide Email Message!";
addActionError(errorMsg);
}
if (file == null) {
String errorMsg = "You must select a file!";
addActionError(errorMsg);
}
}
}
SendEmail.java
Java Struts 2 Framework 2016
package com.giaima; import java.io.File; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class SendEmail { public static void sendEmail(final String emailToAddress, final String emailSubject, final String emailBodyText, final File file, final String fileName) { //Nhập email và password của bạn vào đây. final String username = "Antonio@gmail.com"; final String password = "123456789"; Properties props = new Properties(); props.put("mail.smtp.user", "username"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "25"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.EnableSSL.enable", "true"); props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailToAddress)); message.setSubject(emailSubject); //(Send a Simple E-mail) chỉ gửi Message message.setText(emailBodyText); //(Send Attachment in E-mail) chỉ gửi file MimeBodyPart messageBodyPart = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); DataSource source = new FileDataSource(file); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(fileName); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); //(send the email) gửi email đi Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); } } }
web.xml
Java Struts 2 Framework 2016
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>sendemail.jsp</welcome-file>
</welcome-file-list>
</web-app>
Cấu trúc hiện hành như sau
Chú ý: Gửi Message + File thì không nhận được cả hai. chỉ nhận được loại nào set cuối cùng
Download (zip import NetBeans)
0 nhận xét:
Post a Comment