在实现邮件发送的时候首先需要用到mail.jar开发包,有关mail.jar的下载可以去百度自行下载
下面是邮件发送核心代码
package com.yysj.lhb.action; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.Session;
import javax.mail.Transport; import java.util.List;
import java.util.Properties; /**
* 发送邮箱信息
* @author lhb
*
*/
public class EmailSender { private static final long serialVersionUID = 1L;
private MimeMessage mimeMessage;//MiME邮件对象
private Session session;//邮件会话对象;
private Properties properties;//系统属性
private boolean needAuth = false;//smtp是否需要认证
private String username="";//stmp认证用户和密码
private String password="";
private Multipart multipart;//Multipart对象,邮件内容,标题附件等内容添加到其中后再生成
private String log; public EmailSender() { } public EmailSender(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
} /**
* 设置系统属性
* @param hostName 主机名
*/
public void setSmtpHost(String hostName) {
System.out.println("系统属性:mail.smtp.host="+hostName);
if(properties == null) {
properties = System.getProperties();//获取系统属性对象
}
properties.put("mail.smtp.host", hostName);
properties.put("mail smtp.localhost", "localHostAddress");
} /**
* 创建Mime信息
* @return 成功返回true;否则返回false
*/
public boolean createMimeMessage() {
try {
System.out.println("准备获取邮件会话对象");
session = Session.getDefaultInstance(properties, null);//获取右键会话对象
} catch (Exception e) {
log = "获取邮件会话对象时发生错误!"+e.toString();
System.err.println(log);
return false;
}
try {
mimeMessage = new MimeMessage(session);//创建MIME邮件对象
multipart = new MimeMultipart();//
//Multipart is a container that holds multiple body parts
return true;
} catch (Exception e) {
log = "创建MIME邮件对象失败!"+e.toString();
System.err.println(log);
return false;
}
} /**
* 设置身份认证
* @param need
*/
public void setNeedAuth(boolean need) {
needAuth = need;
System.out.println("设置smtp身份认证:mail.smtp.auth="+need);
if(properties == null) {
properties = System.getProperties();
}
if(needAuth) {
properties.put("mail.smtp.auth", "true");
}else {
properties.put("mail.smtp.auth", "false");
}
}
/**
* 设置用户名和密码
* @param name
* @param pass
*/
public void setNamePass(String name,String pass) {
System.out.println("得到用户名和密码");
username = name;
password = pass;
} /**
* 设置邮件主题
* @param mailSubject
* @return
*/
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题");
try {
mimeMessage.setSubject(MimeUtility.encodeText(mailSubject, "utf-8", "B"));
return true;
} catch (Exception e) {
log = "设置邮件主题发生错误!"+ e;
System.err.println(log);
return false;
}
} /**
* 设置邮件正文
* @param mailBody 正文内容
* @return 设置成功返回true;否则返回false
*/
public boolean setBody(String mailBody) {
try {
System.out.println("设置邮件体格式");
BodyPart bPart = new MimeBodyPart();
//转换中文格式
bPart.setContent("<meta http-equiv=Content-Type content=text/html;charset=utf-8>"+mailBody, "text/html;charset=utf-8");
multipart.addBodyPart(bPart);
return true;
} catch (Exception e) {
log = "设置邮件正文发生错误!"+e;
System.err.println(log);
return false;
}
}
/**
* 设置附件
* @param files
* @return
*/
public boolean setFiles(List<String> files) {
try {
for(String s : files) {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(s);//得到数据源
mimeBodyPart.setDataHandler(new DataHandler(fileDataSource));//得到附件本身并植入BodyPart
mimeBodyPart.setFileName(fileDataSource.getName());//得到文件名同样植入BodyPart
multipart.addBodyPart(mimeBodyPart);
}
return true;
} catch (Exception e) {
log = "增加附件时出错!"+e;
System.err.println(log);
return false;
}
} /**
* 按路径添加附件
* @param path
* @param name
* @return
*/
public boolean addFile(String path,String name) { try {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(path);//得到数据源
mimeBodyPart.setDataHandler(new DataHandler(fileDataSource));//得到附件本身并注入BodyPart
mimeBodyPart.setFileName(MimeUtility.encodeText(name,"utf-8","B"));
multipart.addBodyPart(mimeBodyPart);
return true;
} catch (Exception e) {
log = "增加附件出错"+e;
System.err.println(log);
return false;
} } /**
* 设置发信人
* @param from 发信人名称
* @return
*/
public boolean setFrom(String from) {
System.out.println("设置发信人");
try {
mimeMessage.setFrom(new InternetAddress(from));//设置发信人
return true;
} catch (Exception e) {
log = "设置发言人出错"+e;
return false;
}
} /**
* 设置收件人
* @param to
* @return
*/
public boolean setTo(String to) {
System.out.println("设置收件人");
if(to == null) {
return false;
}
try {
mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
return true;
} catch (Exception e) {
log = "设置收件人错误"+e;
System.err.println(log);
return false;
}
} /**
*
* @param copyto
* @return
*/
public boolean setCopyTo(String copyto) {
if(copyto == null) {
return false;
}
try {
mimeMessage.setRecipients(Message.RecipientType.CC, (Address[])InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} /**
* 发送邮件
* @return
*/
public boolean sendout() {
try {
mimeMessage.setContent(multipart);
mimeMessage.saveChanges();
System.out.println("正在发送邮件...");
Session mailSession = Session.getInstance(properties,null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String)properties.get("mail.smtp.host"), username, password);
transport.sendMessage(mimeMessage, mimeMessage.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch (Exception e) {
log = "邮件发送失败!"+e;
System.err.println(log);
return false;
}
} public String getLog() {
return log;
} }
Java代码
action中的代码如下:
jsp代码
<div class="question_form_1">
<form action="questionForm" method="get" name="form1">
<div class="question_form_1_" style="color: #6e6e6e;">
<h3>有什么需要解惑? </h3>
<textarea class="question_form_youquestion" rows="10" cols="100" id="question_Text" name="question_Text" placeholder="请描述您的问题,比如:心脏前负荷和后负荷有什么区别?动脉血气如何分析? 什么是膜性肾病?"></textarea> <div class="question_form_1_" style="color: #6e6e6e;">
<h3>回复的邮箱或者手机号码</h3>
<input class="question_form_contactyou" id="contact_Customer" name="contact_Customer" type="text" placeholder="请输入您的联系方式" />
</div>
<div class="btn_div">
<input type="submit" id="submit" value="发 送" class="btn" />
</div>
</form>
</div>
页面