ActionForm类
sendMailActionForm.java
package strutsapp.form;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class sendMailActionForm
extends ActionForm {
private String toName;
private String fromName;
private String subjectText;
private String contentText;
public String getContentText() {
return contentText;
}
public String getFromName() {
return fromName;
}
public String getSubjectText() {
return subjectText;
}
public String getToName() {
return toName;
}
public void setToName(String toName) {
this.toName = toName;
}
public void setSubjectText(String subjectText) {
this.subjectText = subjectText;
}
public void setFromName(String fromName) {
this.fromName = fromName;
}
public void setContentText(String contentText) {
this.contentText = contentText;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
}
}
Action类
sendMailAction.java
package strutsapp.action;
import strutsapp.form.*;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import strutsapp.model.sendMail;
public class sendMailAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
sendMailActionForm sendMailActionForm = (sendMailActionForm) actionForm;
String toName = sendMailActionForm.getToName();
String fromName = sendMailActionForm.getFromName();
String subjectText = sendMailActionForm.getSubjectText();
String contentText = sendMailActionForm.getContentText();
sendMail sendmail = new sendMail();
sendmail.sendmail(toName,fromName,subjectText,contentText);
return actionMapping.findForward("sendMailSuccess");
}
}
model类
sendMail.java
package strutsapp.model;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class sendMail {
public void sendmail(String toName, String fromName,
String subjectText,
String contentText) {
//邮件服务器
String host = "127.0.0.1";
//发送方的邮件地址
String to = toName;
//来自方的邮件地址
String from = fromName;
//发送主题
String subject = subjectText;
//发送内容!
String messageText = contentText;
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(sessionDebug);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {
new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
回复 更多评论