用java发邮件,必须要导入新的包
mail.jar – 发邮件的核心包
activation.jar – 对用户和密码加密.
在mail.jar中有三个核心类:
Javax.mail.Session – 是指与邮件服务器会话。整个项目中只要一个就可以了.
Javax.mail.Message(接口) - 准备发送数据信息。
MimeMessage - 可以设置类型的数据信息。
Transport – 它拥有一个方法可以发送Message。
@Test public void sendMail() throws Exception{ //1,声明properties对象放信息 Properties props = new Properties(); //设置连接哪一台服务器 props.setProperty("mail.host", "smtp.163.com"); //设置是否认证: props.setProperty("mail.smtp.auth", "true"); //2,声明用户名和密码 Authenticator auth = new Authenticator(){ //返回用户名和密码对象 @Override protected PasswordAuthentication getPasswordAuthentication() { PasswordAuthentication pa = new PasswordAuthentication("xxxxx@163.com", "123456"); return pa; } }; //3,获取session对象 Session session = Session.getDefaultInstance(props, auth); //设置session为调试模式 session.setDebug(true); //4,声明信息 MimeMessage mm1 = new MimeMessage(session); //5,设置发件人信息 Address form = new InternetAddress("xxxxx@163.com"); mm1.setFrom(form); //6,设置收件人 ,RecipientType:发送,抄送,密送 类型 mm1.setRecipient(RecipientType.TO, new InternetAddress("xxx@qq.com")); //mm1.setRecipient(RecipientType.CC, new InternetAddress(""));//抄送 //7,设置主题 mm1.setSubject("拉面学习通知"); String cont = "请点击 <a href='http://www.fsy158.com/news/31_207'>官网新闻</a>查看祝您发财"; mm1.setContent(cont, "text/html;charset=UTF-8"); //8,发送 Transport.send(mm1); }
其中的mimeType可是text/plain纯文本。
发送附件:
@Test public void sendMailWithFile() throws Exception{ Properties ps = new Properties(); ps.setProperty("mail.host", "smtp.163.com"); ps.setProperty("mail.smtp.auth", "true"); Authenticator auth = new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("xxx@163.com","xxx."); } }; Session session = Session.getDefaultInstance(ps, auth); session.setDebug(true); MimeMessage msg = new MimeMessage(session); Address address = new InternetAddress("xxx@163.com"); msg.setFrom(address); //发送给 msg.setRecipient(RecipientType.TO, new InternetAddress("xxx@qq.com")); msg.setRecipient(RecipientType.BCC, new InternetAddress("xxx@qq.com"));//密送 msg.setRecipient(RecipientType.CC, new InternetAddress("xxx@qq.com"));//抄送 msg.setSubject("$激情小视频*免费观看"); //声明多部件处理 MimeMultipart mm = new MimeMultipart(); MimeBodyPart body1 = new MimeBodyPart(); //设置附件 DataSource ds1 = new FileDataSource(new File("./img/a.jpg")); DataHandler dh1 = new DataHandler(ds1); body1.setDataHandler(dh1); //必须设置名称 body1.setFileName(MimeUtility.encodeText("美女.jpg")); MimeBodyPart body2 = new MimeBodyPart(); DataSource ds2 = new FileDataSource(new File("./img/b.jpg")); DataHandler dh2 = new DataHandler(ds2); body2.setDataHandler(dh2); body2.setFileName(MimeUtility.encodeText("美女2.jpg")); MimeBodyPart body3 = new MimeBodyPart(); DataSource ds3 = new FileDataSource(new File("./img/m.mp3")); DataHandler dh3 = new DataHandler(ds3); body3.setDataHandler(dh3); body3.setFileName(MimeUtility.encodeText("小电影.mp3")); //添加body到mm mm.addBodyPart(body1); mm.addBodyPart(body2); mm.addBodyPart(body3); msg.setContent(mm); //设置发送日期 Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, -5); msg.setSentDate(c.getTime()); //发送 Transport.send(msg); }
可以继续添加MimeBodyPart 来设置正文
MimeBodyPart htmlPart = new MimeBodyPart();
String cont = "请点击 <a href='http://www.fsy158.com/news/31_207'>官网新闻</a>查看祝您发财";
htmlPart.setContent(cont, "text/html;charset=UTF-8");
mm.addBodyPart(htmlPart);