ITeye博客

支持邮件群发,多附件发送的javaMail代码

ShellyLi · 2009-09-02 16:31 · javamail · Eclipse · .net · log4j · Markdown

在网上找了半天才找到了一点比较满意的代码,但是不能直接拿来用,所以就修改了一下,可能有不完美的地方,如果你发现了一定要告诉我哦!有什么需要我帮助的话请发yuxia2217@163.com联系我!大家一起学习一起讨论!

要用到的两个包,因为不能上传附件所以没有传上来,有需要的话,发邮件给我!有时间的话发给你!

用到的包名

activation.jar

mail.jar

java日志文件包可以不用

log4j-1.2.11.jar

源代码如下:

package test;

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;

/**

  • @author 俞晓坚

*update 2007-08-15 * */ public class MailSendtest {

String host="smtp.163.com"; String userName="yuxia2217";//可替换为你自己的邮箱用户名 String password="***************************";//不好意思密码不能给你们看 String from=yuxia2217@163.com //你要发送的邮箱名称 String to;//发给谁 String subject;//邮件议题 String body;//内容

String fileName;//附件路径(注意绝对路径和相对路径的选择)

// 用于保存发送附件的文件名列表 List arrFileName = new List();

public MailSendtest(String to, String subject, String body) throws MalformedURLException, DocumentException { //初始化发件人、收件人、主题等

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.host", "smtp.163.com"); 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; for (int i = 0; i < receiverCount; i++) { address* = new InternetAddress(arr*); } 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 {
 MailSendtest sendmail = new MailSendtest("yuxia2217@163.com,yuzhi2217@163.com",
   "邮件主题", "邮件内容");
 sendmail.attachFile("E:\\eclipse\\startup.jar");//附件地址
 sendmail.attachFile("E:\\eclipse\\notice.html");/ /附件2地址;根据需要可继续添加....................
 sendmail.startSend();
 System.out.println("OK");
} catch (Exception e) {
 e.printStackTrace();
}

} }

评论(1)

  1. #1 楼 · ly5188 · 2012-07-20 16:06

    大虾,例子少了些东西, javax.mail.MessagingException: Could not connect to SMTP host: www.sina.com, port: 25; nested exception is: java.net.ConnectException: Connection timed out: connect

评论功能维护中,当前仅展示历史评论。