在最近项目开发过程中,需要用到JavaMail发邮件。在Java Project下导入官方包mail.jar(截止今天的最新版是1.4.4,下载地址是:http://www.oracle.com/technetwork/java /javamail/index-138643.html),另外需要导入activation-1.1.jar,执行下面的代码,可成功发送邮件。
下面这段代码是网上找的,大家可以参考一下。
代码出处:http://blogold.chinaunix.net/u/15221/showart_1078351.html
import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; /** * 利用java.mail的邮件发送程序 */ public class SendMailTest { public static void main(String[] args) { String title = "SendMailTest_title";// 所发送邮件的标题 String from = "[email protected]";// 从那里发送 String sendTo[] = { "[email protected]", "[email protected]" };// 发送到那里 // 邮件的文本内容,可以包含html标记则显示为html页面 String content = "mail test!!!!!!<br><a href=#>aaa</a>"; // 所包含的附件,及附件的重新命名 String fileNames[] = { "F:\\text1.txt,text1.txt", "F:\\text2.txt,text2.txt" }; try { sendmail(title, from, sendTo, content, fileNames, "text/html;charset=gb2312"); } catch (Exception ex) { ex.printStackTrace(); } } @SuppressWarnings("static-access") public static void sendmail(String subject, String from, String[] to, String text, String[] filenames, String mimeType) throws Exception { // 可以从配置文件读取相应的参数 Properties props = new Properties(); String smtp = "smtp.163.com"; // 设置发送邮件所用到的smtp String servername = "XXXXX"; String serverpaswd = "******"; javax.mail.Session mailSession; // 邮件会话对象 javax.mail.internet.MimeMessage mimeMsg; // MIME邮件对象 props = java.lang.System.getProperties(); // 获得系统属性对象 props.put("mail.smtp.host", smtp); // 设置SMTP主机 props.put("mail.smtp.auth", "true"); // 是否到服务器用户名和密码验证 // 到服务器验证发送的用户名和密码是否正确 Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean( servername, serverpaswd); // 设置邮件会话 mailSession = javax.mail.Session.getInstance(props, (Authenticator) myEmailAuther); // 设置传输协议 javax.mail.Transport transport = mailSession.getTransport("smtp"); // 设置from、to等信息 mimeMsg = new javax.mail.internet.MimeMessage(mailSession); if (from != null && !from.equals("")) { InternetAddress sentFrom = new InternetAddress(from); mimeMsg.setFrom(sentFrom); // 设置发送人地址 } // 收件人 InternetAddress[] sendTo = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { System.out.println("发送到:" + to[i]); sendTo[i] = new InternetAddress(to[i]); } // 抄送 InternetAddress[] sendToCC = new InternetAddress[2]; sendToCC[0] = new InternetAddress("[email protected]"); sendToCC[1] = new InternetAddress("[email protected]"); // 密送 InternetAddress[] sendToBCC = new InternetAddress[2]; sendToBCC[0] = new InternetAddress("[email protected]"); sendToBCC[1] = new InternetAddress("[email protected]"); mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO, sendTo); mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.CC, sendToCC); mimeMsg.setRecipients( javax.mail.internet.MimeMessage.RecipientType.BCC, sendToBCC); mimeMsg.setSubject(subject, "gb2312"); MimeBodyPart messageBodyPart1 = new MimeBodyPart(); messageBodyPart1.setContent(text, mimeType); Multipart multipart = new MimeMultipart();// 附件传输格式 multipart.addBodyPart(messageBodyPart1); for (int i = 0; i < filenames.length; i++) { MimeBodyPart messageBodyPart2 = new MimeBodyPart(); // 选择出每一个附件名 String filename = filenames[i].split(",")[0]; System.out.println("附件名:" + filename); String displayname = filenames[i].split(",")[1]; // 得到数据源 FileDataSource fds = new FileDataSource(filename); // 得到附件本身并设置到BodyPart messageBodyPart2.setDataHandler(new DataHandler(fds)); // 得到文件名同样设置到BodyPart messageBodyPart2.setFileName(MimeUtility.encodeText(displayname)); multipart.addBodyPart(messageBodyPart2); } mimeMsg.setContent(multipart); // 设置信件头的发送日期 mimeMsg.setSentDate(new Date()); mimeMsg.saveChanges(); // 发送邮件 transport.send(mimeMsg); transport.close(); } } /** * 验证类(内部类) * * @author Administrator * */ class Email_Autherticatorbean extends Authenticator { private String m_username = null; private String m_userpass = null; public void setUsername(String username) { m_username = username; } public void setUserpass(String userpass) { m_userpass = userpass; } public Email_Autherticatorbean(String username, String userpass) { super(); setUsername(username); setUserpass(userpass); } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(m_username, m_userpass); } }
接下来,我们把这段代码直接放入已有的J2EE工程中,然后运行,抛出类没有找到的错误:
java.lang.NoClassDefFoundError: com/sun/mail/util/SharedByteArrayInputStream
具体到代码中查看,发现问题出在这行代码:
Session session = Session.getInstance(props);
这行代码的意思是通过javax.mail.Session 创建Session,但是在工程中有两个jar档都有这个类,一个是j2ee.jar,在WebSphere的Library中,另一个是mail.jar,在【Web App Libraries】中,也就是自己导入的,需要调用的jar档。
通过工程的属性的【Java Build Path】的【Order and Export】面板可以看到Library的顺序有问题 ,使用【Up】和【Down】调整一下顺序 ,把【Web App Libraries】放到上面,问题解决!
PS:如果在两个jar在同一个Library中,可以通过修改jar档的名字来调整调用顺序!