当前位置:   article > 正文

Java 002:发邮件,及NoSuchAlgorithmException报错解决办法_java.security.nosuchalgorithmexception: suntls12rs

java.security.nosuchalgorithmexception: suntls12rsapremastersecret keygenera

第1步:首先,项目要引入javax.mail库:

  1. <!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
  2. <dependency>
  3. <groupId>com.sun.mail</groupId>
  4. <artifactId>javax.mail</artifactId>
  5. <version>1.6.2</version>
  6. </dependency>

1.1 或者,你也可以直接下载这2个jar引入到你的java项目中:下载地址当然你也可以Maven仓库下载(javax.mail-1.6.2.jar和activation-1.1.1.jar)。


第2步:写代码,以下是我Hyb测试过(2023/5/12测试可行)可正常发邮件的代码:

  1. import java.util.Properties;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. public final class EmailTest {
  5. public static void main(String[] args) throws Exception {
  6. final String subject = "邮件标题";
  7. final String content = "邮件内容";
  8. final Properties props = new Properties();
  9. props.put("mail.smtp.host", "smtp.126.com");
  10. props.put("mail.smtp.auth", "true");
  11. props.put("mail.smtp.starttls.enable", "true");
  12. props.put("mail.smtp.ssl.trust", "*");
  13. final String to = "收件人@126.com";
  14. final String from = "发件人@126.com";
  15. final String fromPwd = "发件邮箱的授权密码";
  16. final MimeMessage mm = new MimeMessage(Session.getInstance(props, new Authenticator() {
  17. protected PasswordAuthentication getPasswordAuthentication() {
  18. return new PasswordAuthentication(from, fromPwd);
  19. }
  20. })); // 创建MimeMessage
  21. mm.setFrom(new InternetAddress(from)); // 设置发件人、收件人、主题和消息内容
  22. mm.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  23. mm.setSubject(subject);
  24. mm.setText(content);
  25. Transport.send(mm); // 发送消息
  26. }
  27. }

2.1 以上代码,你只需要将收件人发件人发件邮箱的授权密码 改成你的就可以了。


第3步:以上授权密码从哪来?这就需要你登录你的发件邮箱去设置、开启SMTP服务


第4:以上发邮件的代码,发出的邮件内容是纯文本。如果要求邮件内容是html,下面代码可以做到:

  1. import java.util.Properties;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. public final class EmailTest {
  5. public static void main(String[] args) throws Exception {
  6. final String subject = "邮件标题";
  7. final String content = "<font color=red>邮件内容</font>";
  8. final Properties props = new Properties();
  9. props.put("mail.smtp.host", "smtp.126.com");
  10. props.put("mail.smtp.auth", "true");
  11. props.put("mail.smtp.starttls.enable", "true");
  12. props.put("mail.smtp.ssl.trust", "*");
  13. final String to = "收件人@126.com";
  14. final String from = "发件人@126.com";
  15. final String fromPwd = "发件邮箱的授权密码";
  16. final MimeMultipart 正文和附件 = new MimeMultipart();
  17. final MimeBodyPart html = new MimeBodyPart();
  18. html.setContent(content, "text/html;charset=utf-8");
  19. 正文和附件.addBodyPart(html);
  20. final MimeMessage mm = new MimeMessage(Session.getInstance(props, new Authenticator() {
  21. protected PasswordAuthentication getPasswordAuthentication() {
  22. return new PasswordAuthentication(from, fromPwd);
  23. }
  24. })); // 创建MimeMessage
  25. mm.setFrom(new InternetAddress(from)); // 设置发件人、收件人、主题和消息内容
  26. mm.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  27. mm.setSubject(subject);
  28. mm.setContent(正文和附件); // 设置发件内容
  29. Transport.send(mm); // 发送消息
  30. }
  31. }

第5:如果要求邮件内容是html,且带图片,下面代码可以做到:

  1. import java.util.Properties;
  2. import javax.activation.DataHandler;
  3. import javax.activation.FileDataSource;
  4. import javax.mail.Authenticator;
  5. import javax.mail.Message;
  6. import javax.mail.PasswordAuthentication;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.InternetAddress;
  10. import javax.mail.internet.MimeBodyPart;
  11. import javax.mail.internet.MimeMessage;
  12. import javax.mail.internet.MimeMultipart;
  13. public final class EmailTest {
  14. public static void main(String[] args) throws Exception {
  15. final String subject = "邮件标题";
  16. final String content = "<font color=red>邮件内容</font>";
  17. final Properties props = new Properties();
  18. props.put("mail.smtp.host", "smtp.126.com");
  19. props.put("mail.smtp.auth", "true");
  20. props.put("mail.smtp.starttls.enable", "true");
  21. props.put("mail.smtp.ssl.trust", "*");
  22. final String to = "收件人@126.com";
  23. final String from = "发件人@126.com";
  24. final String fromPwd = "发件邮箱的授权密码";
  25. final MimeMultipart 正文和附件 = new MimeMultipart();
  26. 正文和附件.setSubType("related"); //文本和图片内嵌成功
  27. final MimeBodyPart image = new MimeBodyPart();
  28. image.setDataHandler(new DataHandler(new FileDataSource("D:/aaa.png")));
  29. image.setContentID("a.png"); //设置图片id,这个非常重要。如果这个a.png跟下面的不一样,那这个图片就成邮件附件了
  30. 正文和附件.addBodyPart(image);
  31. final MimeBodyPart html = new MimeBodyPart();
  32. html.setContent(content + "<br/><img src='cid:a.png'>", "text/html;charset=utf-8");
  33. 正文和附件.addBodyPart(html);
  34. final MimeMessage mm = new MimeMessage(Session.getInstance(props, new Authenticator() {
  35. protected PasswordAuthentication getPasswordAuthentication() {
  36. return new PasswordAuthentication(from, fromPwd);
  37. }
  38. })); // 创建MimeMessage
  39. mm.setFrom(new InternetAddress(from)); // 设置发件人、收件人、主题和消息内容
  40. mm.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  41. mm.setSubject(subject);
  42. mm.setContent(正文和附件); // 设置发件内容
  43. Transport.send(mm); // 发送消息
  44. }
  45. }

第6:如果要求邮件带附件,只要让上面代码setContentID("a.png")中,a.png不要被引用就可以了,当然网上还有其它办法,具体自己查查。反正以上3份代码,你copy后,改下收件人、发件人、发件邮箱授权密码,代码就可以成功运行的,本人Hyb测试多次没有问题。


第7:以上代码在eclipse中运行,没有一点问题。但如果脱离IDE环境,改成命令行部署的方式,就会报错。

7.1 命令行执行为:java -Djava.ext.dirs=D:/K/lib/ KxSnapshot 3600 1600 Z:/K/

      关于这条命令, 参数-Djava.ext.dirs指定了KxSnapshot.class依赖jar的存放目录。

7.2 但该命令在执行时会报错:NoSuchAlgorithmException: SunTls12RsaPremasterSecret KeyGenerator not available

  1. javax.mail.MessagingException: Could not convert socket to TLS
  2. at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2155)
  3. at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:752)
  4. at javax.mail.Service.connect(Service.java:388)
  5. at javax.mail.Service.connect(Service.java:246)
  6. at javax.mail.Service.connect(Service.java:195)
  7. at javax.mail.Transport.send0(Transport.java:254)
  8. at javax.mail.Transport.send(Transport.java:124)
  9. at KxSnapshot.emailSend(KxSnapshot.java:897)
  10. at KxSnapshot.emailFindKDJ紫色连续2点升降(KxSnapshot.java:859)
  11. at KxSnapshot.main(KxSnapshot.java:105)
  12. Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error
  13. at sun.security.ssl.RSAClientKeyExchange.<init>(Unknown Source)
  14. at sun.security.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
  15. at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
  16. at sun.security.ssl.Handshaker.processLoop(Unknown Source)
  17. at sun.security.ssl.Handshaker.process_record(Unknown Source)
  18. at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
  19. at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
  20. at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  21. at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  22. at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:626)
  23. at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:553)
  24. at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2150)
  25. ... 9 more
  26. Caused by: java.security.NoSuchAlgorithmException: SunTls12RsaPremasterSecret KeyGenerator not available
  27. at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:169)
  28. at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
  29. at sun.security.ssl.JsseJce.getKeyGenerator(Unknown Source)
  30. ... 21 more

7.3 对于发邮件时报出NoSuchAlgorithmException,我Hyb实际的解决办法也简单:

      将jdk1.8.0_112/jre/lib/ext目录下的5个jar,copy到上面命令中的D:/K/lib/下。这5个jar是:

      ​​​​​​​localedata.jar , sunpkcs11.jar , sunmscapi.jar , sunjce_provider.jar , dnsns.jar  

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/570638
推荐阅读
相关标签
  

闽ICP备14008679号