如何更正无效协议:null使用javax.mail发送邮件 [英] How to correct Invalid Protocol: null sending mail using javax.mail

查看:2031
本文介绍了如何更正无效协议:null使用javax.mail发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

运输运输...... 我检索到这个错误:

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!! :)

编辑:

如果我创建一个main并启动该方法来发送邮件,它运行良好!
我将邮件发送到邮件文件夹后出现问题:

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:

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

我认为例外情况因为我已经开店获取收件箱邮件,以便我以这种方式编辑:

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

非常奇怪....

以这种方式解决:

我修改了这一行

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

Session mailSession = Session.getInstance(props);


推荐答案

你可以尝试另一种方式:

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

这篇关于如何更正无效协议:null使用javax.mail发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆