发送邮件工具类
发布日期:2021-06-30 18:41:08 浏览次数:8 分类:技术文章

本文共 1805 字,大约阅读时间需要 6 分钟。

/** * @创建者 CSDN_LQR * @描述 发送邮件工具类 */public class SendJMail {
public static boolean sendMail(String email, String emailMsg) { String from = "15812345678@163.com"; // 邮件发送人的邮件地址 String to = email; // 邮件接收人的邮件地址 final String username = "15812345678@163.com"; //发件人的邮件帐户 final String password = "忘了"; //发件人的邮件密码 //定义Properties对象,设置环境信息 Properties props = System.getProperties(); //设置邮件服务器的地址 props.setProperty("mail.smtp.host", "smtp.163.com"); // 指定的smtp服务器 props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp");//设置发送邮件使用的协议 //创建Session对象,session对象表示整个邮件的环境信息 Session session = Session.getInstance(props); //设置输出调试信息 session.setDebug(true); try { //Message的实例对象表示一封电子邮件 MimeMessage message = new MimeMessage(session); //设置发件人的地址 message.setFrom(new InternetAddress(from)); //设置主题 message.setSubject("用户激活"); //设置邮件的文本内容 //message.setText("Welcome to JavaMail World!"); message.setContent((emailMsg), "text/html;charset=utf-8"); //从session的环境中获取发送邮件的对象 Transport transport = session.getTransport(); //连接邮件服务器 transport.connect("smtp.163.com", 25, username, password); //设置收件人地址,并发送消息 transport.sendMessage(message, new Address[]{
new InternetAddress(to)}); transport.close(); return true; } catch (MessagingException e) { e.printStackTrace(); return false; } }}

注意:上面的例子是以163为例,如需使用其他邮箱(新浪邮箱、QQ邮箱等)需要修改对应的mail.smtp.host和登录账号。该工具类需要mail.jar的支持。懒人地址:

转载地址:https://linqiarui.blog.csdn.net/article/details/53326957 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:搭建Nginx服务器
下一篇:ftp文件上传及下载工具类

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月23日 09时23分21秒