?? 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 + -