本文实例为大家分享了java实现发送邮件的工具类,供大家参考,具体内容如下
sendemailutil
1 2 3 4 5 |
<dependency> <groupid>javax.mail</groupid> <artifactid>mail</artifactid> <version> 1.4 . 5 </version> </dependency> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import javax.mail.address; import javax.mail.message; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import com.sun.mail.util.mailsslsocketfactory; import java.io.fileoutputstream; import java.io.outputstream; import java.text.simpledateformat; import java.util.date; import java.util.properties;
public class sendmailutil {
//邮件服务器主机名 // qq邮箱的 smtp 服务器地址为: smtp.qq.com private static string myemailsmtphost = "smtp.qq.com" ;
//发件人邮箱 private static string myemailaccount = "xxxxxxxxxx@xx.com" ;
//发件人邮箱密码(授权码) //在开启smtp服务时会获取到一个授权码,把授权码填在这里 private static string myemailpassword = "xxxxxxxxxxxx" ;
/** * 邮件单发(自由编辑短信,并发送,适用于私信) * * @param toemailaddress 收件箱地址 * @param emailtitle 邮件主题 * @param emailcontent 邮件内容 * @throws exception */ public static void sendemail(string toemailaddress, string emailtitle, string emailcontent) throws exception{
properties props = new properties();
// 开启debug调试 props.setproperty( "mail.debug" , "true" );
// 发送服务器需要身份验证 props.setproperty( "mail.smtp.auth" , "true" );
// 端口号 props.put( "mail.smtp.port" , 465 );
// 设置邮件服务器主机名 props.setproperty( "mail.smtp.host" , myemailsmtphost);
// 发送邮件协议名称 props.setproperty( "mail.transport.protocol" , "smtp" );
/**ssl认证,注意腾讯邮箱是基于ssl加密的,所以需要开启才可以使用**/ mailsslsocketfactory sf = new mailsslsocketfactory(); sf.settrustallhosts( true );
//设置是否使用ssl安全连接(一般都使用) props.put( "mail.smtp.ssl.enable" , "true" ); props.put( "mail.smtp.ssl.socketfactory" , sf);
//创建会话 session session = session.getinstance(props);
//获取邮件对象 //发送的消息,基于观察者模式进行设计的 message msg = new mimemessage(session);
//设置邮件标题 msg.setsubject(emailtitle);
//设置邮件内容 //使用stringbuilder,因为stringbuilder加载速度会比string快,而且线程安全性也不错 stringbuilder builder = new stringbuilder();
//写入内容 builder.append( "\n" + emailcontent);
//写入我的官网 builder.append( "\n官网:" + "https://www.hbuecx.club" );
//定义要输出日期字符串的格式 simpledateformat sdf = new simpledateformat( "yyyy-mm-dd hh:mm:ss" );
//在内容后加入邮件发送的时间 builder.append( "\n时间:" + sdf.format( new date()));
//设置显示的发件时间 msg.setsentdate( new date());
//设置邮件内容 msg.settext(builder.tostring());
//设置发件人邮箱 // internetaddress 的三个参数分别为: 发件人邮箱, 显示的昵称(只用于显示, 没有特别的要求), 昵称的字符集编码 msg.setfrom( new internetaddress(myemailaccount, "你好!" , "utf-8" ));
//得到邮差对象 transport transport = session.gettransport();
//连接自己的邮箱账户 //密码不是自己qq邮箱的密码,而是在开启smtp服务时所获取到的授权码 //connect(host, user, password) transport.connect( myemailsmtphost, myemailaccount, myemailpassword);
//发送邮件 transport.sendmessage(msg, new address[] { new internetaddress(toemailaddress) });
//将该邮件保存到本地 outputstream out = new fileoutputstream( "myemail.eml" ); msg.writeto(out); out.flush(); out.close();
transport.close(); } } |
1 2 3 4 |
//toemailaddress 目标邮箱地址 //emailtitle 邮件标题 //emailcontent 邮件内容 sendmailutil.sendemail(toemailaddress, emailtitle, emailcontent); |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/qq_38928944/article/details/82788904
查看更多关于java工具类SendEmailUtil实现发送邮件的详细内容...