EmailAction.java
The next step is to
create an Action method that takes care of sending the email. Let us create a
new class called EmailAction.java.
Action file have to save at
src/vaibhav package.
Code-
package vaibhav;
import
java.io.File;
import
java.io.IOException;
import
javax.mail.MessagingException;
import
javax.mail.internet.AddressException;
import
org.apache.commons.io.FileUtils;
public class
SendEmailAction {
// SMTP properties - fetched from struts.xml
private String host;
private String port;
private String userName;
private String password;
private String userName1;
private String password1;
// file upload properties - fetched by interceptor fileUpload
private File fileUpload;
private String fileUploadFileName;
private String fileUploadContentType;
// e-mail fields - fetched from EmailForm.jsp
private String recipient;
private String subject;
private String message;
public String Login()
{
return "success";
}
public String doSendEmail() throws
IOException, AddressException, MessagingException {
File saveFile = null;
String tempPath =
System.getProperty("java.io.tmpdir");
saveFile = new File(tempPath + File.separator
+ fileUploadFileName);
FileUtils.copyFile(fileUpload,
saveFile);
System.out.println("file"+fileUpload);
EmailUtility.sendEmail(host, port,
userName1, password1, recipient,
subject, message,
saveFile);
System.out.println("save
file"+saveFile);
if (saveFile != null) {
saveFile.delete();
}
return "success";
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload)
{
this.fileUpload = fileUpload;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String
fileUploadFileName) {
this.fileUploadFileName =
fileUploadFileName;
}
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void
setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType =
fileUploadContentType;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient)
{
this.recipient = recipient;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserName1() {
return userName1;
}
public void setUserName1(String userName1)
{
this.userName1 = userName1;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1)
{
this.password1 = password1;
}
}
Output:-
Login successuful
When Email successfully send.
Give Error when unsuccessful.