注意需要最新的mail.jar
package xueyuan.commons;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-2-15
* Time: 14:02:18
* To change this template use File | Settings | File Templates.
*/
import java.io.PrintStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.*;
import javax.mail.*;
public class SendMail
{
private MimeMessage mimeMsg;
private Session session;
private Properties props;
private boolean needAuth;
private String username;
private String password;
private Multipart mp;
public SendMail(String smtp)
{
needAuth = false;
username = "";
password = "";
setSmtpHost(smtp);
createMimeMessage();
}
public void setSmtpHost(String hostName)
{
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if(props == null)
props = System.getProperties();
props.put("mail.smtp.host", hostName);
// System.out.println(props.get("mail.smtp.host"));
//gmail需要的协议
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// System.out.println(props.get("mail.smtp.socketFactory.class"));
//gmail特殊端口
props.put("mail.smtp.port","465");
}
public boolean createMimeMessage()
{
try
{
System.out.println("准备获取邮件会话对象!");
PopupAuthenticator popAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popAuthenticator.performCheck(username,password);
session = Session.getDefaultInstance(props, popAuthenticator);
}
catch(Exception e)
{
System.err.println("获取邮件会话对象时发生错误!" + e);
// e.printStackTrace();
return false;
}
System.out.println("准备创建MIME邮件对象!");
try
{
mimeMsg = new MimeMessage(session);
mp = new MimeMultipart();
}
catch(Exception e)
{
System.err.println("创建MIME邮件对象失败!" + e);
// e.printStackTrace();
return false;
}
return true;
}
public void setNeedAuth(boolean need)
{
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if(props == null)
props = System.getProperties();
if(need)
//设置需要协议支持
props.put("mail.smtp.auth", "true");
else
props.put("mail.smtp.auth", "false");
}
public void setNamePass(String name, String pass)
{
// System.out.println("程序得到用户名与密码");
username = name;
password = pass;
}
public boolean setSubject(String mailSubject)
{
// System.out.println("设置邮件主题!");
try
{
mimeMsg.setSubject(mailSubject);
}
catch(Exception e)
{
// System.err.println("设置邮件主题发生错误!");
return false;
}
return true;
}
public boolean setBody(String mailBody)
{
try
{
// "设置邮件体格式"
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>" + mailBody, "text/html;charset=GB2312");
mp.addBodyPart(bp);
}
catch(Exception e)
{
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
return true;
}
public boolean addFileAffix(String filename)
{
System.out.println("增加邮件附件:" + filename);
try
{
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
}
catch(Exception e)
{
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
return true;
}
public boolean setFrom(String from)
{
System.out.println("设置发信人!");
try
{
mimeMsg.setFrom(new InternetAddress(from));
}
catch(Exception e)
{
return false;
}
return true;
}
public boolean setTo(String to)
{
System.out.println("设置收信人");
if(to == null)
return false;
try
{
mimeMsg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(to));
}
catch(Exception e)
{
return false;
}
return true;
}
public boolean setCopyTo(String copyto)
{
System.out.println("发送附件到");
if(copyto == null)
return false;
try
{
mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC, InternetAddress.parse(copyto));
}
catch(Exception e)
{
return false;
}
return true;
}
public boolean sendout()
{
try
{
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
PopupAuthenticator popAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popAuthenticator.performCheck(username,password);
Session mailSession = Session.getInstance(props,popAuthenticator);
//协议种类
Transport transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"), username, password);
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(javax.mail.Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
}
catch(Exception e)
{
System.err.println("邮件发送失败!" + e.getMessage());
// e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
// TODO 自动生成方法存根
SendMail themail = new SendMail("smtp.gmail.com");
String mailbody = "尊敬的" ;
themail.setNeedAuth(true);
//邮件的title
themail.setSubject("找回密码");
//邮件的内容
themail.setBody(mailbody);
//发给谁
themail.setTo("
[email protected]");
//从哪个邮件发与下面的setNamepass的需要一致
themail.setFrom("
[email protected]");
themail.setNamePass("
[email protected]","***");
themail.sendout();
}
}
调用的方法(也就是邮件的验证)
package xueyuan.commons;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-2-15
* Time: 15:23:08
* To change this template use File | Settings | File Templates.
*/
import javax.mail.*;
import javax.mail.internet.*;
public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}