错误:看不到软件包sun.net.smtp [英] error: package sun.net.smtp is not visible
问题描述
我们正在将一些旧代码移至java11.我们正在创建一个smtp客户端.当我们使用java11时,coe编译会失败.
We are moving some old code to java11.We are creating a smtp client. The coe compilation fails when we use java11.
error: package sun.net.smtp is not visible
[javac] sun.net.smtp.SmtpClient SMTP = new sun.net.smtp.SmtpClient(SMTP_SERVER);
[javac] ^
[javac] (package sun.net.smtp is declared in module java.base, which
does not export it)
似乎已从java11中删除了smtp软件包支持.任何建议都会有所帮助.
looks like smtp package support is removed from the java11. Any suggestions will be helpful.
关于,Akj
推荐答案
您可以使用JavaMail API发送电子邮件进行排序.转到下面的链接并下载.jar文件,并将其添加到您的项目中.如果没有,您可以将其添加为Maven依赖项. https://mvnrepository.com/artifact/javax.mail/mail/1.4.7
You can use JavaMail API to send email to get this sorted. Go to below link and download the .jar file and add to your project. If not you can add it as a maven dependency. https://mvnrepository.com/artifact/javax.mail/mail/1.4.7
与SMTP服务器通信并按照以下方式发送电子邮件的示例代码.
A sample code to talk to SMTP server and send email as per below.
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@abcmail.com"));
message.setRecipients(
Message.RecipientType.TO, InternetAddress.parse("to@abcmail.com"));
message.setSubject("Mail Subject");
String msg = "This is a sample email ";
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
Transport.send(message);
您的配置可以通过Java属性对象完成
Your configurations can be done with a Java Properties object
Properties prop = new Properties();
prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.abcmail.com");
prop.put("mail.smtp.port", "25");
prop.put("mail.smtp.ssl.trust", "smtp.abcmail.com");
这篇关于错误:看不到软件包sun.net.smtp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!