小编jas*_*sop的帖子

如何更正 Invalid Protocol: null 使用 javax.mail 发送邮件

我正在尝试以这种方式发送邮件:

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "out.alice.it");
props.setProperty("mail.user", "mymail@domain.it");
props.setProperty("mail.password", "*****");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("Host", "Name"));
Run Code Online (Sandbox Code Playgroud)

在该行Transport transport...我检索到此错误:

javax.mail.NoSuchProviderException: Invalid protocol: null
    at javax.mail.Session.getProvider(Session.java:440)
    at javax.mail.Session.getTransport(Session.java:659)
    at javax.mail.Session.getTransport(Session.java:640)
    at javax.mail.Session.getTransport(Session.java:626)
    at Mail.sendMail(Mail.java:151)
Run Code Online (Sandbox Code Playgroud)

我该如何解决?有人能帮我吗?谢谢!!:)

编辑:

如果我创建一个 main 并启动该方法来发送邮件,效果很好!当我将邮件读入邮件文件夹后,我的问题出现了:

Properties properties = System.getProperties();  
properties.setProperty("mail.store.protocol", "imap");  
Session session = Session.getDefaultInstance(properties, null);
Store store = session.getStore("pop3");
store.connect("pop3.domain.it", "mail@domain.it", "****");  
Folder inbox = store.getFolder("inbox");  
FlagTerm ft = new …
Run Code Online (Sandbox Code Playgroud)

java jakarta-mail nosuchproviderexception

4
推荐指数
1
解决办法
2万
查看次数