2005.04.17发表于blog.csdn.net/zxub
没什么好玩的了,最近看到好多地方有发邮件的东东,正好在看Java,就用Java写了,需要下载mail.jar和activation.jar,此外,用到我前次写的操作XML文件的一个类(上篇文章),好了,不多说了,代码贴贴,怎么用就看自己了:
//JavaMail中一些我们需要的类
//1.Properties
//
//JavaMail需要Properties来创建一个session对象,其属性值就是发送邮件的主机,如:
//
//Properties props = new Properties ();
//props.put("mail.smtp.host", "smtp.xxxx.com");//可以换上你的smtp主机名,就像你在OutLook中设置smtp主机名一样。
//props.put("mail.smtp.auth", "true"); //通过验证
//
//2.Session
//
//所有的基于JavaMail的程序都至少需要一个或全部的对话目标。
//
//Session session = Session.getInstance(props, null);
//
//3.MimeMessage
//
//信息对象将把你所发送的邮件真实的反映出来。
//
//MimeMessage msg = new MimeMessage(session);
//
//4.Transport
//
//邮件的发送是由Transport来完成的:
//
//Transport.send(msg);
import java.awt.List;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.dom4j.DocumentException;
/*
* Created on 2005-4-14 21:27:16
*
*/
/**
* @author zxub
*
*/
public class MailSender {
String host;
String userName;
String password;
String from;
String to;
String subject;
String body;
String fileName;
// 用于保存发送附件的文件名列表
List arrFileName = new List();
public MailSender(String to, String subject, String body)
throws MalformedURLException, DocumentException {
//初始化发件人、收件人、主题等
OperaXML OX = new OperaXML();
this.from = OX.getNodeValue("config.xml", "config/Host1/From");
this.host = OX.getNodeValue("config.xml", "config/Host1/SmtpHost");
this.userName = OX.getNodeValue("config.xml", "config/Host1/UserName");
this.password = OX.getNodeValue("config.xml", "config/Host1/Password");
this.to = to;
this.subject =subject;
this.body = body;
}
// 用于收集附件名
public void attachFile(String fileName) {
this.arrFileName.add(fileName);
}
// 开始发送
public boolean startSend() {
//创建Properties对象
Properties props = System.getProperties();
//创建信件服务器
props.put("mail.smtp.host", this.host);
props.put("mail.smtp.auth", "true"); //通过验证
//得到默认的对话对象
Session session = Session.getDefaultInstance(props, null);
try {
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(this.from));
if (this.to.trim().length() > 0) {
String[] arr = this.to.split(",");
//int ReceiverCount=1;
int receiverCount = arr.length;
if (receiverCount > 0) {
InternetAddress[] address = new InternetAddress[receiverCount];
for (int i = 0; i < receiverCount; i++) {
address[i] = new InternetAddress(arr[i]);
}
msg.addRecipients(Message.RecipientType.TO, address);
} else {
System.out
.println("None receiver! The program will be exit!");
System.exit(0);
}
} else {
System.out.println("None receiver! The program will be exit!");
System.exit(0);
}
msg.setSubject(subject);
//后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();
//获取附件
int FileCount = this.arrFileName.getItemCount();
if (FileCount > 0) {
for (int i = 0; i < FileCount; i++) {
MimeBodyPart mbp = new MimeBodyPart();
//选择出附件名
fileName = arrFileName.getItem(i).toString();
//得到数据源
FileDataSource fds = new FileDataSource(fileName);
//得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
//得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(this.body);
mp.addBodyPart(mbp);
//移走集合中的所有元素
arrFileName.removeAll();
//Multipart加入到信件
msg.setContent(mp);
} else {
//设置邮件正文
msg.setText(this.body);
}
//设置信件头的发送日期
msg.setSentDate(new Date());
msg.saveChanges();
//发送信件
Transport transport = session.getTransport("smtp");
transport.connect(this.host, this.userName, this.password);
transport.sendMessage(msg, msg
.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
return false;
}
return true;
}
public static void main(String[] args) {
try {
MailSender sendmail = new MailSender("邮箱地址1,邮箱地址2",
"邮件主题", "邮件内容");
sendmail.attachFile("E:\\eclipse\\workspace\\MailSender\\MailSender.rar");//附件地址
//sendmail.attachFile("附件2地址");....................
sendmail.startSend();
System.out.println("OK");
} catch (Exception e) {
e.printStackTrace();
}
}
}