javamail ssl

    xiaoxiao2022-07-12  144

    public void startSend(String mailTitle, String mailBody, String[] receives, File[] attachments) throws UnsupportedEncodingException, javax.mail.MessagingException { if (smtpip == null || smtpip.trim().length() == 0) throw new IllegalArgumentException("登陆SMTP服务器地址为空"); if (smtpport == null || smtpport.trim().length() == 0) throw new IllegalArgumentException("登陆SMTP端口为空"); if (senderMail == null || senderMail.trim().length() == 0) throw new IllegalArgumentException("发件人邮件地址为空"); if (senderUserName == null || senderUserName.trim().length() == 0) throw new IllegalArgumentException("登陆SMTP用户名为空"); if (senderUserPass == null || senderUserPass.trim().length() == 0) throw new IllegalArgumentException("登陆SMTP密码为空"); if (senderName == null || senderName.trim().length() == 0) throw new IllegalArgumentException("发件人为空"); if (receives == null || receives.length == 0) throw new IllegalArgumentException("收件人为空"); if (mailTitle == null || mailTitle.trim().length() == 0) throw new IllegalArgumentException("邮件主题为空"); if (mailBody == null || mailBody.trim().length() == 0) throw new IllegalArgumentException("邮件内容为空"); // 组成邮件群发地址 InternetAddress[] addrs = new InternetAddress[receives.length]; for (int i = 0; i < receives.length; i++) { addrs[i] = new InternetAddress(receives[i]); } Properties mailProps = new Properties(); Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); //设置邮件会话参数 //邮箱的发送服务器地址 mailProps.put("mail.smtp.host", "smtp.qiye.163.com"); mailProps.put("mail.smtp.socketFactory", "javax.net.ssl.SSLSocketFactory"); mailProps.put("mail.smtp.socketFactory.fallback", "false"); //邮箱发送服务器端口,这里设置为465端口 //mailProps.setProperty("mail.smtp.port", "25"); mailProps.put("mail.smtp.socketFactory.port", "994"); mailProps.put("mail.smtp.auth", "true"); mailProps.put("mail.smtp.timeout", 0); mailProps.put("mail.smtp.ssl.enable", "true"); mailProps.put("mail.store.protocol", "smtp"); // 赋值 /*mailProps.put("mail.smtp.host", smtpip); mailProps.put("mail.store.protocol", "smtp");// 设置协议 mailProps.put("mail.smtp.auth", "true"); mailProps.put("mail.smtp.port", smtpport); mailProps.put("mail.smtp.starttls.enable", "true");*/ // 邮件登陆的用户名和密码 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(senderUserName, senderUserPass); } }; Session mailSession = Session.getInstance(mailProps, authenticator); //Session.getInstance(mailProps,new Email_Autherticatorbean(senderUserName, K.k(senderUserPass))); // 邮件设置 Transport tran = mailSession.getTransport("smtp"); tran.connect(); mailSession.setDebug(true); MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(senderMail, senderName, "GBK")); msg.setRecipients(Message.RecipientType.TO, addrs); msg.setSubject(mailTitle); // msg.setHeader("Client-Ip", request.getRemoteAddr()); msg.setSentDate(new Date(System.currentTimeMillis())); StringBuffer msgBody = new StringBuffer(mailBody); // 增加邮件的文本 MimeMultipart multi = new MimeMultipart(); BodyPart textBodyPart = new MimeBodyPart(); String sendContent = "<div style='font-size:20px;color:blue;'>" + msgBody.toString() + "</div>"; // textBodyPart.setContent(msgBody.toString(), "text/html;charset=UTF-8"); textBodyPart.setContent(sendContent, "text/html;charset=utf-8"); textBodyPart.setHeader("Content-Type", "text/html;charset=utf-8"); // textBodyPart.setText(msgBody.toString()); multi.addBodyPart(textBodyPart); // 增加附件 for (int i = 0; attachments != null && i < attachments.length; i++) { if (attachments[i] != null && attachments[i].exists()) { FileDataSource fds = new FileDataSource(attachments[i]); BodyPart fileBodyPart = new MimeBodyPart(); fileBodyPart.setDataHandler(new DataHandler(fds)); fileBodyPart.setFileName(new String(attachments[i].getName() .getBytes("GB2312"), "8859_1")); multi.addBodyPart(fileBodyPart); } } msg.setContent(multi); msg.saveChanges(); tran.sendMessage(msg, msg.getAllRecipients()); tran.close(); }

    注意备忘的坑

    1.当前代码对应的依赖版本是1.5.4

    <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.4</version> </dependency>

    对应的区别就是:

    mailProps.put("mail.smtp.socketFactory", "javax.net.ssl.SSLSocketFactory");

    key中不能带有.class

    对于其他的更高或者其他版本的内容没有进行详细的研究.

    我们使用的是163企业邮箱,查询邮箱的ssl端口使用的是:

    https://qiye.163.com/help/client-profile.html

    进行查询.

     

    关于配置文件:

    mail: smtp: smtp.qiye.163.com smtpport: 25 smtpsslport: 994 username: eport@qiye.163.com password: jtBQDmU34n4YqtKjy #注意此处的设置是从邮箱设置中获取的授权码,这里并不是邮箱用户的密码

    总之就是很多坑.

    最新回复(0)