gpt4 book ai didi

JavaMail API 错误 (javax.mail.NoSuchProviderException : invalid provider)

转载 作者:太空宇宙 更新时间:2023-11-04 14:25:17 26 4
gpt4 key购买 nike

我正在尝试使用 JavaMail API 创建一个程序,但是,我不断收到以下错误消息。

javax.mail.NoSuchProviderException: invalid provider
at javax.mail.Session.getTransport(Session.java:738)
at javax.mail.Session.getTransport(Session.java:682)
at javax.mail.Session.getTransport(Session.java:662)
at EmailAutoResponder2.main(EmailAutoResponder2.java:56)

我无法通过在线阅读来解决这个问题,因为他们所有的解决方案仍然给了我同样的信息。

这是Java代码:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class EmailAutoResponder2 {

public static void main(String[] args) {

String to = "username@videotron.ca";
String from = "username@videotron.ca";

Properties properties = System.getProperties();

properties.setProperty("mail.store.protocol", "imaps");

Session session1 = Session.getInstance(properties);

//If email received by specific user, send particular response.
Properties props = new Properties();

props.put("mail.imap.auth", "true");
props.put("mail.imap.starttls.enable", "true");
props.put("mail.imap.host", "imap.videotron.ca");
props.put("mail.imap.port", "143");

Session session2 = Session.getInstance(props, new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username@videotron.ca", "password");
}
});

try {
Store store = session2.getStore("imap");
store.connect("imap.videotron.ca", "username@videotron.ca", "password");
Folder fldr = store.getFolder("Inbox");
fldr.open(Folder.READ_ONLY);
Message msgs[] = fldr.getMessages();
for(int i = 0; i < msgs.length; i++){
System.out.println(InternetAddress.toString(msgs[i].getFrom()));

if (InternetAddress.toString(msgs[i].getFrom()).startsWith("Name")){

MimeMessage message = new MimeMessage(session1);

message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Subject");
message.setText("Message");

String protocol = "imap";
props.put("mail." + protocol + ".auth", "true");

Transport t = session2.getTransport("imap");
try {
t.connect("username@videotron.ca", "password");
t.sendMessage(message, message.getAllRecipients());
}
finally {
t.close();
}

}
}

}

catch(MessagingException mex){
mex.printStackTrace();
}

catch(Exception exc) {

}

}

}

谢谢!

最佳答案

您正在连接到本地主机来发送消息。您的本地计算机上有运行邮件服务器吗?可能不会。您需要设置 mail.smtp.host 属性。您可能还需要提供邮件服务器的用户名和密码;请参阅JavaMail FAQ .

关于JavaMail API 错误 (javax.mail.NoSuchProviderException : invalid provider),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26747053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com