为大家介绍javaMail组件的使用,首先是发送普通的邮件
package javamail;
import java.io.IOException;
import java.util.*;
import javax.mail.internet.*;
import javax.mail.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaMailServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InternetAddress[] address = null;
request.setCharacterEncoding("GBK");
//使用qq服务器
String mailserver = "服务器的名称";
String From = request.getParameter("From");
String to = request.getParameter("To");
String Subject = request.getParameter("Subject");
String MessageText = request.getParameter("Message");
boolean sessionDebug = false;
try{
//设定Mail的服务器和传输协议
Properties props = System.getProperties();
props.put("mail.host",mailserver);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth","true");//安全协议,若你不是使用免费服务器可不用
/*
* 产生新的session服务
* 安全协议用内部类,参数表示QQ账号和密码
* 在普遍的免费服务器中大多数都要进行安全验证
*/
javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props,new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("账号","密码");
}
});
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
//设定传送邮件的发信人
msg.setFrom(new InternetAddress(From));
//设定传送邮件至收信人的信息
address = InternetAddress.parse(to,false);
msg.setRecipients(Message.RecipientType.TO,address);
//设定信中的主题
msg.setSubject(Subject);
// 设定传送信的MIME Type
msg.setText(MessageText);
Transport.send(msg);
System.out.println("邮件已顺利发送");
}catch(Exception e){
e.printStackTrace();
}
}
}
接下来的是带有附件的邮件,在这里我将使用的是oreilly组件进行上传
public class JavaMailServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
InternetAddress[] address = null;
MultipartRequest multi = new MultipartRequest(request,".",5*1024*1024,"gb2312");
String mailserver = "服务器";
String From = multi.getParameter("From");
String to = multi.getParameter("To");
String Subject = multi.getParameter("Subject");
String type = multi.getParameter("Type");
String messageText = multi.getParameter("Message");
String FileName = multi.getFilesystemName("FileName");
boolean sessionDebug = false;
try{
Properties props = System.getProperties();
props.put("mail.host",mailserver);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth","true");
//产生新的session服务
javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props,new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("账号","密码");
}
});
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
//设定传送邮件的发信人
msg.setFrom(new InternetAddress(From));
//设定传送邮件至收信人的信息
address = InternetAddress.parse(to,false);
msg.setRecipients(Message.RecipientType.TO,address);
//设定信中的主题
msg.setSubject(Subject);
//设定送信的时间
msg.setSentDate(new Date());
if(FileName!=null){
File file = new File(FileName);
//如果有附件,就将邮件内容部分存起来
MimeBodyPart mbp1 = new MimeBodyPart();
//设定邮件内容的类型为text/plain或text/html
mbp1.setContent(messageText,type+";charset=gb2312");
//接下来是对附件进行处理
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(FileName);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(MimeUtility.encodeText(fds.getName(),"gb2312","B"));
//最后再将两者集成起来,当做一份邮件送出
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
}else{
//若没有附件,当做一份邮件送出
msg.setContent(messageText,type+";charset=gb2312");
}
Transport.send(msg);
System.out.println("邮件已顺利发送");
}catch(Exception e){
e.printStackTrace();
}
}
}
以上是使用javaMail组件来发送,如果大家有更好的方法希望留言哦!也可以把意见发到我的邮箱上
[email protected] !本人在这里求一种通过socket的方法来发送邮件的!谢谢