java 如何更正无效协议:使用 javax.mail 发送空邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11265755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:27:06  来源:igfitidea点击:

How to correct Invalid Protocol: null sending mail using javax.mail

javajavax.mailnosuchproviderexception

提问by HymanTurky

i'm trying to send mail in this way:

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

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "out.alice.it");
props.setProperty("mail.user", "[email protected]");
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"));

On the row Transport transport...i retrieve this error:

在这一行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)

How can i resolve? Can someone help me? Thanks!! :)

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

EDIT:

编辑:

If i create a main and launch that method to send mail, it works good! My problem borns after i read mail into mail folder:

如果我创建一个 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", "[email protected]", "****");  
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!
   }
}

I Thought the exception borns cause i've opened store to get inbox mail so i edit in this way:

我认为异常的原因是因为我打开了商店来接收收件箱邮件,所以我以这种方式进行编辑:

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!
}

very strange....

很奇怪....

SOLVED IN THIS WAY:

以这种方式解决:

i modified this row

我修改了这一行

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

to

Session mailSession = Session.getInstance(props);

采纳答案by Pavel K.

you might try another way:

你可以尝试另一种方式:

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

回答by Dr.Octoboz

I was getting this same error, and my problem was I forgot to include:

我遇到了同样的错误,我的问题是我忘了包括:

props.put("mail.transport.protocol", "smtp");