Java Web stmp发送带附件邮件(附SSL版)
程序员文章站
2023-12-18 17:34:16
本文实例为大家分享了java web stmp发送带附件邮件的具体代码,供大家参考,具体内容如下
public class mailfilesendutils {...
本文实例为大家分享了java web stmp发送带附件邮件的具体代码,供大家参考,具体内容如下
public class mailfilesendutils { private properties props; //系统属性 private session session; //邮件会话对象 private mimemessage mimemsg; //mime邮件对象 private multipart mp; //multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成mimemessage对象 /** * constructor * @param */ public mailfilesendutils(){ props = system.getproperties(); props.put("mail.smtp.auth","false"); session = session.getdefaultinstance(props, null); session.setdebug(true); mimemsg = new mimemessage(session); mp = new mimemultipart(); } /** * constructor * @param smtp 邮件发送服务器 */ public mailfilesendutils(string smtp, string username, string password){ props = system.getproperties(); props.put("mail.smtp.auth","true"); props.put("mail.smtp.host", smtp); props.put("username", username); props.put("password", password); session = session.getdefaultinstance(props, null); session.setdebug(true); mimemsg = new mimemessage(session); mp = new mimemultipart(); } /** * 发送邮件 */ public boolean sendmail(string from, string[] to, string subject, string content, string filename) { try { //设置发信人 mimemsg.setfrom(new internetaddress(from)); //设置接收人 for (int i = 0; i < to.length; i++) { mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(to[i])); } //设置抄送人 // for (int i = 0; i < copyto.length; i++) { // mimemsg.setrecipients(message.recipienttype.cc, internetaddress.parse(copyto[i])); // } //设置主题 mimemsg.setsubject(subject); //设置正文 bodypart bp = new mimebodypart(); bp.setcontent(content, "text/html;charset=utf-8"); mp.addbodypart(bp); //设置附件 bp = new mimebodypart(); filedatasource fileds = new filedatasource(filename); bp.setdatahandler(new datahandler(fileds)); bp.setfilename(mimeutility.encodetext(fileds.getname(),"utf-8","b")); mp.addbodypart(bp); mimemsg.setcontent(mp); mimemsg.savechanges(); //发送邮件 if(props.get("mail.smtp.auth").equals("true")){ transport transport = session.gettransport("smtp"); transport.connect((string)props.get("mail.smtp.host"), (string)props.get("username"), (string)props.get("password")); transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.to)); // transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.cc)); transport.close(); }else{ transport.send(mimemsg); } system.out.println("邮件发送成功"); } catch (messagingexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } return true; } // public void tosendmail(sendmailparam sendmailparam){ // mailfilesendutils email = new mailfilesendutils(sendmailparam.getsmtp(), sendmailparam.getusername(), sendmailparam.getpassword()); // email.sendmail(sendmailparam.getfrom(), sendmailparam.getto(), sendmailparam.getsubject(), sendmailparam.getcontent(), sendmailparam.getfilepath()); // } public static void main(string[] args) { string smtp = "smtp.exmail.qq.com"; string username = "发送的邮箱账号"; string password = "发送的邮箱密码"; string from = "发送的邮箱"; string[] to = {"接收邮件的邮箱"}; // string[] copyto = {"抄送的邮箱"}; string subject = "主题6"; string content = "邮件内容6"; string filename = "附件的文件"; mailfilesendutils email = new mailfilesendutils(smtp, username, password); // email.sendmail(from, to, copyto, subject, content, filename); email.sendmail(from, to, subject, content, filename); } }
(附:ssl版)
public class mailfilesendutils { private properties props; //系统属性 private session session; //邮件会话对象 private mimemessage mimemsg; //mime邮件对象 private multipart mp; //multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成mimemessage对象 /** * constructor * @param */ public mailfilesendutils(){ props = system.getproperties(); props.put("mail.smtp.auth","false"); session = session.getdefaultinstance(props, null); session.setdebug(true); mimemsg = new mimemessage(session); mp = new mimemultipart(); } /** * constructor * @param smtp 邮件发送服务器 */ public mailfilesendutils(string smtp, string username, string password){ security.addprovider(new com.sun.net.ssl.internal.ssl.provider()); final string ssl_factory = "javax.net.ssl.sslsocketfactory"; props = system.getproperties(); mailsslsocketfactory sf = null; try { sf = new mailsslsocketfactory(); } catch (generalsecurityexception e) { } sf.settrustallhosts(true); props.put("mail.smtp.auth","true"); props.put("mail.smtp.host", smtp); props.put("mail.smtp.socketfactory.class", ssl_factory); props.put("mail.smtp.socketfactory.fallback", "false"); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.ssl.socketfactory", sf); // props.put("username", username); // props.put("password", password); session = session.getinstance(props, new authenticator() { @override protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(username, password); } }); session.setdebug(true); mimemsg = new mimemessage(session); mp = new mimemultipart(); } /** * 发送邮件 */ public boolean sendmail(string from, string[] to, string subject, string content, string filename) { try { //设置发信人 mimemsg.setfrom(new internetaddress(from)); //设置接收人 for (int i = 0; i < to.length; i++) { mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(to[i])); } //设置抄送人 // for (int i = 0; i < copyto.length; i++) { // mimemsg.setrecipients(message.recipienttype.cc, internetaddress.parse(copyto[i])); // } //设置主题 mimemsg.setsubject(subject); //设置正文 bodypart bp = new mimebodypart(); bp.setcontent(content, "text/html;charset=utf-8"); mp.addbodypart(bp); //设置附件 bp = new mimebodypart(); filedatasource fileds = new filedatasource(filename); bp.setdatahandler(new datahandler(fileds)); bp.setfilename(mimeutility.encodetext(fileds.getname(),"utf-8","b")); mp.addbodypart(bp); mimemsg.setcontent(mp); mimemsg.savechanges(); //发送邮件 if(props.get("mail.smtp.auth").equals("true")){ transport transport = session.gettransport("smtp"); transport.connect((string)props.get("mail.smtp.host"), (string)props.get("username"), (string)props.get("password")); transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.to)); // transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.cc)); transport.close(); }else{ transport.send(mimemsg); } system.out.println("邮件发送成功"); } catch (messagingexception e) { e.printstacktrace(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } return true; } public boolean tosendmail(sendmailparam sendmailparam){ mailfilesendutils email = new mailfilesendutils( sendmailparam.getsmtp(), sendmailparam.getusername(), sendmailparam.getpassword()); email.sendmail( sendmailparam.getfrom(), sendmailparam.getto(), sendmailparam.getsubject(), sendmailparam.getcontent(), sendmailparam.getfilepath()); return true; } // public static void main(string[] args) { // string smtp = "smtp.mxhichina.com"; // string username = "邮箱"; // string password = "邮箱密码"; // string from = "谁去发"; // string[] to = {"发给谁"}; //// string[] copyto = {"抄送的邮箱"}; // string subject = "huawei"; // string content = "邮件内容6666"; // string filename = "gdt-3583118353-ad-20170823.xls"; // mailfilesendutils email = new mailfilesendutils(smtp, username, password); //// email.sendmail(from, to, copyto, subject, content, filename); // email.sendmail(from, to, subject, content, filename); // } }
在项目中使用这套工具,main方法我注释掉,然后使用tosendmail(sendmailparam sendmailparam)。
这里定义的sendmailparam 为:
public class sendmailparam { private string smtp; private string username; private string password; private string from;//发送人 private string[] to;//接收人 // string[] copyto = {"909891736@qq.com"}; private string subject;//邮件主题 private string content;//邮件内容 private string filepath;//文件拿到的路径 public sendmailparam(){ this.smtp = "smtp.exmail.qq.com";//例子 this.username = "邮箱账号"; this.password = "邮箱密码"; this.from = "邮箱"; this.subject = ""; this.content = ""; this.filepath = ""; } public string getsmtp() { return smtp; } public void setsmtp(string smtp) { this.smtp = smtp; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getfrom() { return from; } public void setfrom(string from) { this.from = from; } public string[] getto() { return to; } public void setto(string[] to) { this.to = to; } public string getsubject() { return subject; } public void setsubject(string subject) { this.subject = subject; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } public string getfilepath() { return filepath; } public void setfilepath(string filepath) { this.filepath = filepath; } }
maven依赖包
<dependency> <groupid>javax.mail</groupid> <artifactid>mail</artifactid> <version>1.4.7</version> </dependency>
gradle依赖包
compile "javax.mail:mail:1.4.7"
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。