📄 mailaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.MailForm;
/**
* MyEclipse Struts
* Creation date: 10-08-2008
*
* XDoclet definition:
* @struts.action path="/mail" name="mailForm" input="/index.jsp" scope="request" validate="true"
*/
public class MailAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MailForm mailForm = (MailForm) form;// TODO Auto-generated method stub
boolean mailsent=false;//发送成功与失败标志符
/*
*
*需要获取收信人email地址并引注掉下句
*String toAddr = "laoliljw@163.com";
*
*/
String toAddr = "laoliljw@163.com";
String subject = "password";//email主题
//Here write your own email or get it from a parameter
String fromAddr = "laoliljw@bupt.cn";//email发信人地址
//生成10位随机密码
int i,j;
String pwd="";
for(i=0;i<10;i++)
{
j=(int)(Math.random()*(122-65))+65;
pwd+=(char)j;
}
String body = "你的帐号密码为"+pwd;//email内容
/*
*
* 添加登陆密码更新到数据库中的SQL
*
*/
try {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.bupt.cn");//第二个参数是smtp服务器地址
props.put("mail.smtp.auth", "true"); //开启smtp验证方式
// Here we specify the SMTP server through
// which the mail should be delivered
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(fromAddr));// Specify the From Address
InternetAddress[] tos = InternetAddress.parse(toAddr);// Format the To Address
msg.setRecipients(Message.RecipientType.TO, tos);// Specify the To Address
msg.setSubject(subject); // Specify the Subject
msg.setText(body);// Specify the Body
Transport tr=session.getTransport("smtp");
tr.connect("smtp.bupt.cn", "laoliljw@bupt.cn", "laoliljw");
//连接服务器并登陆 三个参数分别为smtp服务器地址、发件人Email地址、登陆密码
msg.saveChanges();
tr.sendMessage(msg,msg.getAllRecipients());//发送Email
mailsent=true;
} catch (Exception e) {
mailsent=false;
e.printStackTrace();
}
if(mailsent==true)
return(mapping.findForward("1"));//发送成功跳转路径选择
else
return(mapping.findForward("2"));//发送失败跳转路径选择
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -