1.导入activation.jar和mail.jar连个文件
2.关键代码如下:
package com.test;
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;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendEmial {
public static void main(String[] args) {
//创建邮件会话
Properties prop = new Properties();
prop.put("mail.transport.protocol","smtp");
prop.put("mail.smtp.port","25");
prop.put("mail.smtp.host", "127.0.0.1");
Session s = Session.getDefaultInstance(prop);
//新建一个消息对象
Message msg = new MimeMessage(s);
try {
//发信人
Address from = new InternetAddress("a@zsw.com");
//收信人
Address to = new InternetAddress("b@zsw.com");
msg.setFrom(from);
msg.setRecipient(RecipientType.TO, to);
msg.setSubject("标题");//标题
msg.setText("ddddddddddddddddddddddddddddddddddddddddBB"); //邮件内容
Transport.send(msg); //发送邮件
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}