JavaMail实现发送邮件功能
程序员文章站
2024-02-24 21:55:22
本文实例为大家分享了javamail实现发送邮件功能的具体代码,供大家参考,具体内容如下
用java发邮件,必须要导入新的包
&nb...
本文实例为大家分享了javamail实现发送邮件功能的具体代码,供大家参考,具体内容如下
用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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。