form class="form-horizontal" id=“estForm” action="/tools/toolServlet?type=est" method="post" enctype="multipart/form-data">
div class="control-group">
label class="control-label" for="email">Email/label>
div class="controls">
input type="text" id="email" name="email" placeholder="Email" onblur="checkEmail()">
span class="help-inline">/span>
/div>
/div>
div class="control-group">
label class="control-label" for="file">File/label>
div class="controls">
input type="file" name="file" id="file" onchange="getFileInfo()"/>
span class="help-inline">/span>
/div>
/div>
!-- 隱藏文件域 begin
div class="control-group" id="hiddenFileDiv" style="display: none;">
label class="control-label" for="file">File/label>
div class="controls">
input type="file" name="file1" id="file1" />
span class="help-inline">/span>
/div>
/div>-->
!-- 隱藏文件域 end
div class="control-group">
label class="control-label" for="file">crossmatch/label>
div class="controls">
select name="crossmatch" id="crossmatch">
option value="Y">Y/option>
option value="N">N/option>
/select>
span class="help-inline">/span>
/div>
/div>-->
div class="control-group">
div class="controls">
!-- a href="javascript:void(0);" id="upload" class="btn">submit/a>-->
button type="submit" class="btn" id="upload">submit/button>
button type="reset" class="btn" id="reset">reset/button>
/div>
/div>
/form>
public class EmailAttachService {
private static String host = "smtp.163.com";
private static String username = "";
private static String password = "";
private static String mailSubject = "";
public static Vector vfile = new Vector();
//添加附件
public static void addAttachemnt(String fPath){
vfile.add(fPath);
}
//發(fā)送郵件
public static void sendMail(String emailTo,String msg) {
// vfile 附件文件集合
try {
Properties props = new Properties();// 獲取系統(tǒng)環(huán)境
Authenticator auth = new EmailAuthenticator(username, password);// 進(jìn)行郵件服務(wù)用戶認(rèn)證
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, auth);
// 設(shè)置session,和郵件服務(wù)器進(jìn)行通訊
MimeMessage message = new MimeMessage(session);
// 設(shè)置郵件發(fā)送者的地址
message.setFrom(new InternetAddress(username));
// 設(shè)置郵件接收的地址
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
// 設(shè)置郵件主題
message.setSubject(mailSubject);
// 構(gòu)造Multipart
Multipart mp = new MimeMultipart();
// 向Multipart添加正文
MimeBodyPart content = new MimeBodyPart();
content.setContent(msg, "text/html;charset=gb2312");
mp.addBodyPart(content);
// 向Multipart添加附件
Enumeration efile = vfile.elements();
while(efile.hasMoreElements()){
MimeBodyPart fattach = new MimeBodyPart();
String fName = efile.nextElement().toString();
FileDataSource fds = new FileDataSource(fName);
fattach.setDataHandler(new DataHandler(fds));
fattach.setFileName(MimeUtility.encodeWord(fds.getName(), "GB2312",null));
mp.addBodyPart(fattach);
}
vfile.removeAllElements();
message.setContent(mp);
// 設(shè)置郵件發(fā)送時(shí)期
message.setSentDate(new Date());
message.saveChanges();
//發(fā)送郵件
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}