开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 30954|回复: 1
收起左侧

[已解决] 求大神给一个SpringMVC框架下能购发送邮件的java例子

 关闭 [复制链接]
结帖率:56% (5/9)
发表于 2018-6-17 01:29:32 | 显示全部楼层 |阅读模式   广东省东莞市
5精币
求大神给一个SpringMVC框架下能发送邮件的java例子,今天搞了一天,一启动spring容器就各种报错,好烦      我快奔溃了,网上的例子不是只能本地发送就是不能被是spring容器所管理 然后各种报错

Bean创建异常   空指针异常      各种异常   烦死了   做一回伸手党    那错误分析起来简直要命

最佳答案

查看完整内容

静态方法 也是之前的代码里面翻出来的
结帖率:0% (0/1)
发表于 2018-6-17 01:29:33 | 显示全部楼层   广东省广州市
  1.    public static void sendEmail(emailEntity record)  {
  2.         try{
  3.             // 创建邮件配置
  4.             Properties props = new Properties();
  5.             props.setProperty("mail.transport.protocol", "smtp"); // 使用的协yi(JavaMail规范要求)
  6.             props.setProperty("mail.smtp.host", "smtp.qq.com"); // 发件人的邮箱的 SMTP 服务器地址
  7.             props.setProperty("mail.smtp.port", "465");
  8.             props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  9.             props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
  10.             props.setProperty("mail.smtp.ssl.enable", "true");// 开启ssl
  11.             // 根据邮件配置创建会话,注意session别导错包
  12.             Session session = Session.getDefaultInstance(props);
  13.             // 开启debug模式,可以看到更多详细的输入日志
  14.             session.setDebug(false);
  15.             //创建邮件

  16.             MimeMessage msg = new MimeMessage(session);
  17.             // address邮件地址, personal邮件昵称, charset编码方式
  18.             InternetAddress fromAddress = new InternetAddress(record.getFromcode(),
  19.                     record.getAddressor(), "utf-8");
  20.             // 设置发送邮件方
  21.             msg.setFrom(fromAddress);
  22.             InternetAddress receiveAddress = new InternetAddress(
  23.                     record.getTocode(), record.getRecipients(), "utf-8");
  24.             // 设置邮件接收方
  25.             msg.setRecipient(Message.RecipientType.TO, receiveAddress);
  26.             // 设置邮件标题
  27.             msg.setSubject(record.getSubject(), "utf-8");
  28.             // 设置显示的发件时间
  29.             msg.setSentDate(new Date());

  30.             MimeMultipart mp = new MimeMultipart();
  31.             BodyPart bp = new MimeBodyPart();
  32.             bp.setContent(record.getContent(), "text/html;charset=utf-8");
  33.             mp.addBodyPart(bp);
  34.             if(!UtilString.isEmpty(record.getFileName())){
  35.                 bp = new MimeBodyPart();
  36.                 FileDataSource fds = new FileDataSource(record.getFileName());
  37.                 bp.setDataHandler(new DataHandler(fds));
  38.                 bp.setFileName(MimeUtility.encodeText(fds.getName(), "UTF-8", "B"));
  39.             }
  40.             mp.addBodyPart(bp);
  41.             msg.setContent(mp);
  42.             // 保存设置

  43.             msg.saveChanges();
  44.             //获取传输通道
  45.             Transport transport = session.getTransport();
  46.             transport.connect("smtp.qq.com",record.getFromcode(), record.getPassword());
  47.             //连接,并发送邮件
  48.             transport.sendMessage(msg, msg.getAllRecipients());
  49.             transport.close();
  50.         }catch(Exception e){
  51.             if(e instanceof javax.mail.AuthenticationFailedException){
  52.                 throw new crmException("请检查发件人邮箱设置是否正确");
  53.             }
  54.             throw new crmException(e.getMessage());
  55.         }

  56.     }
复制代码



静态方法   也是之前的代码里面翻出来的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表