第一步:建立一个java项目
第二步:引入mail.jar包
由简单到复杂,第一种邮件发送的形式
package com.li72.email;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* 类名称:MyEmailClient
* 类描述:
* 创建人:li72
* 创建时间:2012-4-22 上午09:35:46
* 修改人:li72
* 修改时间:2012-4-22 上午09:35:46
* 修改备注:
*/
public class SimpleEmailClient {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");//必须 普通客户端
props.setProperty("mail.transport.protocol", "smtp");//必须选择协议
Session session = Session.getDefaultInstance(props);
session.setDebug(true);//设置debug模式 在控制台看到交互信息
Message msg = new MimeMessage(session); //建立一个要发送的信息
msg.setText("li72 welcome ");//设置简单的发送内容
msg.setFrom(new InternetAddress("********@163.com"));//发件人邮箱号
Transport transport = session.getTransport();//发送信息的工具
transport.connect("smtp.163.com", 25, "**发件人邮箱号*******", "***密码****");//发件人邮箱号 和密码
transport.sendMessage(msg, new Address[] { new InternetAddress(
"********@163.com") });//对方的地址
transport.close();
}
}
第二种邮件发送的形式
/**
*
*/
package com.li72.email;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* 类名称:OtherEmail
* 类描述:
* 创建人:li72
* 创建时间:2012-4-22 上午10:48:18
* 修改人:li72
* 修改时间:2012-4-22 上午10:48:18
* 修改备注:
*/
public class OtherEmail {
/**
*
*
*/
public static void main(String[] args) throws Exception{
Properties props=new Properties();
props.setProperty("mail.smtp.auth", "true");//必须 普通客户端
props.setProperty("mail.transport.protocol", "smtp");//必须选择协议
props.setProperty("mail.host", "smtp.163.com");
Session session=Session.getDefaultInstance(props,
new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("发件人邮箱","密码");
}
});
session.setDebug(true);
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress("发件人邮箱"));
msg.setSubject("主题");
msg.setRecipients(RecipientType.TO,InternetAddress.parse("l收件人,收件人") );//多个收件人
msg.setContent("连接", "text/html;charset=gbk");
Transport.send(msg);
}
}
第三种 带附件的
/**
*
*/
package com.li72.email;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* 类名称:AnthoerEmail
* 类描述:
* 创建人:li72
* 创建时间:2012-4-22 下午02:29:06
* 修改人:li72
* 修改时间:2012-4-22 下午02:29:06
* 修改备注:
*/
public class AnthoerSimpleEmail {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Properties props=new Properties();
props.setProperty("mail.smtp.auth", "true");//必须 普通客户端
props.setProperty("mail.transport.protocol", "smtp");//必须选择协议
props.setProperty("mail.host", "smtp.163.com");
Session session=Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication("发件人邮箱","密码");
}
});
session.setDebug(true);
Message msg=new MimeMessage(session);
MimeMultipart mimeMultipart=new MimeMultipart();
MimeBodyPart mimeBodyPart=new MimeBodyPart();
mimeBodyPart.setText("hello");
MimeBodyPart attach1=new MimeBodyPart();
mimeMultipart.addBodyPart(mimeBodyPart);
DataSource ds1 = new FileDataSource("F:\\六级.txt");
DataHandler dh1=new DataHandler(ds1);
attach1.setDataHandler(dh1);
attach1.setFileName("reg.txt");
mimeMultipart.addBodyPart(attach1);
msg.setContent(mimeMultipart);
msg.saveChanges();
msg.setFrom(new InternetAddress("发件人"));
Date d=new Date();
msg.setSubject(""+d.getDate());
msg.setRecipient(RecipientType.TO, new InternetAddress("收件人"));
Transport.send(msg);
}
}
第四种 ,用commons-email-1.1.jar,还需要两个额外的包mail.jar,activation.jar包
public static void mySend() {
try {
SimpleEmail email = new SimpleEmail(); // 构造一个mail对象
email.setHostName("li72-pc");// 设置主机名
email.addTo("XXXXXXX@163.com", "li");// 设置发对象
email.setFrom("XXXXX@163.com", "my");// 设置发送人
email.setSubject("邮件测试");// 设置主题
email.setCharset("GBK");// 设置发送使用的字符集
String content = "测试内容是我自己的";// 内容
email.setContent(content, "text/plain;charset=GBK");// 设置内容
email.send();// 发送
} catch (EmailException e) {
e.printStackTrace();
}
}