package com.javamail.test;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class TestJavaMail {
public static void main(String[] arge) throws Exception {
String from = "[email protected]";
String to = "[email protected]";
String subject = "Test mail";
String body = "A text mail";
Properties props = System.getProperties();
// 设置SMTP邮件服务器:
props.put("mail.smtp.host", "mail.trendcom.com.cn");
// SMTP服务器需要验证:
props.put("mail.smtp.auth", "true");
//设置邮件传输协议为:smtp
props.put( "mail.transpost.protocol", "smtp");
//设置邮件服务器端口
props.put("mail.smtp.port", "25");
// 传入用户名和口令:
Session session = Session.getDefaultInstance(props,
new PasswordAuthenticator("[email protected]", "pizza"));
// 创建新邮件:
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(body);
msg.setSentDate(new Date());
// 发送:
Transport.send(msg);
}
}
class PasswordAuthenticator extends Authenticator {
private String username;
private String password;
public PasswordAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
package com.javamail.test;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class TestJavaMailFile {
public static void main(String[] args) {
TestJavaMailFile mailFile=new TestJavaMailFile();
mailFile.setHost("mail.trendcom.com.cn");
mailFile.setUser("[email protected]");
mailFile.setPassword("pizza");
mailFile.setFrom("[email protected]");
mailFile.setTo("[email protected]");
mailFile.setSubject("附件邮件");
mailFile.setText("请主意查收附件");
mailFile.attachfile("test.doc");
System.out.println(mailFile.startSend());
}
class PasswordAuthenticator extends Authenticator {
private String username;
private String password;
public PasswordAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
// 定义发件人、收件人、主题等
String user="";
String password="";
String to = "";
String from = "";
String host = "";
String filename = "";
String subject = "";
String text="";
// 用于保存发送附件的文件名的集合
Vector file = new Vector();
public TestJavaMailFile(){
}
// 该方法用于收集附件名
public void attachfile(String fname) {
file.addElement(fname);
}
// 开始发送信件的方法
public boolean startSend() {
// 创建Properties对象
Properties props = System.getProperties();
// 创建信件服务器
props.put("mail.smtp.host", host);
// SMTP服务器需要验证:
props.put("mail.smtp.auth", "true");
//设置邮件传输协议为:smtp
props.put( "mail.transpost.protocol", "smtp");
//设置邮件服务器端口
props.put("mail.smtp.port", "25");
// 得到默认的对话对象
Session session = Session.getDefaultInstance(props,new PasswordAuthenticator(getUser(),getPassword()));
try {
// 创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// 后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();
// 利用枚举器方便的遍历集合
Enumeration efile = file.elements();
// 检查序列中是否还有更多的对象
while (efile.hasMoreElements()) {
MimeBodyPart mbp = new MimeBodyPart();
// 选择出每一个附件名
filename = efile.nextElement().toString();
// 得到数据源
FileDataSource fds = new FileDataSource(filename);
// 得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
// 得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
// 移走集合中的所有元素
file.removeAllElements();
//创建新的MimeBodyPart
MimeBodyPart messageBodyPart = new MimeBodyPart();
//把正文信息附加到MimeBodyPart
messageBodyPart.setText(text);
//Multipart添加正文信息
mp.addBodyPart(messageBodyPart);
// Multipart加入到信件
msg.setContent(mp);
// 设置信件头的发送日期
msg.setSentDate(new Date());
// 发送信件
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
return false;
}
return true;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
测试了一下,不行呀,报Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: mail.sina.com.cn, port: 25;
解决方案:邮件smtp:服务器不正确
如果新浪邮件服务器:smtp.sina.com
同样的错误可能是邮件服务器不稳定造成的!
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
如果你用myEclipse进行开发的话,运行时可能会出现以下的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
原因是jar包版本不统一,解决方法如下:
删除Java EE 5 Libraries/javaee.jar/mail里的包有东西.
具体方法如下:
用rar打开X:/Program Files/MyEclipse 6.0/myeclipse/eclipse/plugins/com.genuitec.eclipse.j2eedt.core_6.0.1.zmyeclipse601200710/data/libraryset/EE_5/javaee.jar
,然后删除mail,一切就ok了.