首先需要下载两个jar包,
一个是mail.jar,下载地址:http://www.javasoft.com/products/javamail/
另一个是activation.jar包,地址:http://java.sun.com/beans/glasgow/jaf.html
并设置到classpath中,
基本代码如下
package groupsms;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
//import SendMail.Email_Autherticatorbean;
/**
*
* @author sweater
* @version 1.0, 09/01/04
* @since JDK1.4.2
*
* 利用JavaMail,发送电子邮件
*
*/
public class SendMail{
public String to;
public String from;
public String subject;
public String mailhost;
public String content;
public String user;
public String password;
public String cc;
public String bcc;
public SendMail(String to,String from,String subject,String mailhost,
String content,String user,String password,String cc,
String bcc){
this.to = to;
this.from = from;
this.subject = subject;
this.mailhost = mailhost;
this.content = content;
this.user = user;
this.password = password;
this.cc = cc;
this.bcc = bcc;
/*
System.out.println(user);
System.out.println(password);
System.out.println(subject);
System.out.println(mailhost);
System.out.println(to);
System.out.println(from);
*/
}
public int sendMailBySMTP(){
int flag = -1;
//MIME邮件对象
MimeMessage mimeMsg = null;
//邮件会话对象
Session session = null;
try {
Properties props = System.getProperties(); //获得系统属性
props.put("mail.smtp.host", mailhost); //设置SMTP主机
props.put("mail.smtp.auth","true"); //设置身份验证为真,若须身份验证则必须设为真
//获得邮件会话对象
//session = Session.getDefaultInstance(props,null);
//注意下面这行的 Session.getDefaultInstance 方法的第二个参数
session = Session.getDefaultInstance(props, new Email_Autherticatorbean(user, password));
//创建MIME邮件对象
mimeMsg = new MimeMessage(session);
//设置发信人
mimeMsg.setFrom(new InternetAddress(from));
//设置收信人
if(to!=null){
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
}
//设置抄送人
if(cc!=null){
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
//设置暗送人
if(bcc!=null){
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
}
//设置邮件主题
mimeMsg.setSubject(subject,"GBK");
//设置邮件内容
mimeMsg.setText(content,"GBK");
//发送日期
mimeMsg.setSentDate(new Date());
//发送邮件
Transport.send(mimeMsg);
flag = 120;
//System.out.println( "email send!");
}catch(MessagingException msgingE){
flag = 121;
System.out.println(msgingE.getMessage());
}
return flag;
}
class Email_Autherticatorbean extends javax.mail.Authenticator{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username){
m_username = username;
}
public void setUserpass(String userpass){
m_userpass = userpass;
}
public Email_Autherticatorbean(){
super();
}
public Email_Autherticatorbean(String username, String userpass){
super();
setUsername(username);
setUserpass(userpass);
}
//一定要有这个方法,它是在需要身份验证时自动被调用的
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(m_username,m_userpass);
}
}
public static void main(String[] args) {
SendMail sm = new SendMail("sswt@163.com,sswt@163.com","sswt@163.com","sswt@163.com","smtp.163.com",
"smtp.163.com","sswt","密码","sswt@163.com","sswt@163.com");
}
}