java - 如何更正无效协议(protocol) : null sending mail using javax. 邮件

标签 java jakarta-mail nosuchproviderexception

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

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"));

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)

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

编辑:

如果我创建一个 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 FlagTerm(new Flags(Flags.Flag.SEEN), false);
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.search(ft);
for(Message message:messages) {
   if(message.getFrom()[0].toString().equals("aMail")){
      sendMail(message.getFrom()[0].toString());//Error!
   }
}

我认为异常出生是因为我已经打开商店来接收收件箱邮件所以我以这种方式编辑:

ArrayList<String> reply = new ArrayList<String();
for(Message message:messages) {
    if(message.getFrom()[0].toString().equals("aMail")){
        reply.add(message.getFrom()[0].toString());
    }
}
store.close();
for(String mail : reply){
   sendMail(mail); // ERROR AGAIN!
}

很奇怪....

以这种方式解决:

我修改了这一行

Session mailSession = Session.getDefaultInstance(props, null);

Session mailSession = Session.getInstance(props);

最佳答案

你可以尝试另一种方式:

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "out.alice.it");
props.setProperty("mail.smtp.auth", "true");
final PasswordAuthentication auth = new PasswordAuthentication(smtpUser, stmpPassword);
Session mailSession = Session.getDefaultInstance(props, new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() { return auth; }
});
MimeMessage message = ....;
// compose the message
Transport.send(message);

关于java - 如何更正无效协议(protocol) : null sending mail using javax. 邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11265755/

相关文章:

java - 大 o 符号和递归函数

java - 线程 "main"javax.mail.AuthenticationFailedException 中的异常

email - 通过 imap 使用 Javamail 发送电子邮件

java - Java Mail中是否有像SpecifiedPickupDirectory这样的配置SMTP?

Java 加密提供程序 - 奇怪的堆栈跟踪

java - System.gc()什么时候做某事?

java - 在资源包中使用 html 标签

java - Tomcat session.ser