gpt4 book ai didi

java mail API转发邮件功能

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

我正在开发一个独立的java实用程序,它将连接到任何组织的现有邮箱,并从收件箱读取/检查邮件,如果任何邮件的地址字段(“TO和CC”)是否超过2000个字符的字符限制……如果超过限制,那么它将转发同一封邮件,否则电子邮件ID将不执行任何操作……我已经编写了下面给出的转发邮件功能的代码……工作正常……邮件正在转发,但显示异常……。请指出我不足的地方......

public class ForwardMail {

public static void main(String[] args){
Properties properties = new Properties();
properties.put("mail.store.protocol", "pop3");
properties.put("mail.pop3s.host", "mailcluster.mycompany.com");
properties.put("mail.pop3s.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
properties.put("mail.smtp.auth", "true"); /* may be commented later*/
properties.put("smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "mailcluster.mycompany.com");
properties.put("mail.smtp.port", "25");

// Session session = Session.getDefaultInstance(properties);
Session session = Session.getInstance(properties,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myname@mycompany.com","mypassword@123");
}
});
try{
// session.setDebug(true);
// Get a Store object and connect to the current host
Store store = session.getStore("pop3s");
store.connect("mailcluster.mycompany.com", "myname@mycompany.com","mypassword@123");


// Create a Folder object and open the folder
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Message[] messages = folder.getMessages();
if (messages.length != 0) {

for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
// Get all the information from the message
// String from = InternetAddress.toString(message.getFrom());
String from = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
if (from != null) {
System.out.println("From:" + from);
}
/*String replyTo = InternetAddress.toString(message.getReplyTo());
if (replyTo != null) {
System.out.println("Reply-to: " + replyTo);
}*/
String to = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
if (to != null) {
System.out.println("To: " + to);
}

String subject = message.getSubject();
if (subject != null) {
System.out.println("Subject: " + subject);
}
/*Date sent = message.getSentDate();
if (sent != null) {
System.out.println("Sent: " + sent);
}*/
System.out.print("Do you want to reply [y/n] : ");
String ans = reader.readLine();
if ("Y".equals(ans) || "y".equals(ans)) {
Message forward = new MimeMessage(session);
// Fill in header
forward.setRecipients(Message.RecipientType.TO,InternetAddress.parse(from));
forward.setSubject("Fwd: " + message.getSubject());
forward.setFrom(new InternetAddress(to));

// Create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
// Create a multipart message
Multipart multipart = new MimeMultipart();
// set content
messageBodyPart.setContent(message, "message/rfc822");
// Add part to multi part
multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
forward.setContent(multipart);
forward.saveChanges();
// Transport.send(forward);
// Send the message by authenticating the SMTP server
// Create a Transport instance and call the sendMessage
Transport t = session.getTransport("smtp");
try {
//connect to the smpt server using transport instance
t.connect("myname@mycompany.com", "mypassword@123");
t.sendMessage(forward, forward.getAllRecipients());

} finally {
t.close();
}

System.out.println("message forwarded successfully....");

// close the store and folder objects
folder.close(false);
store.close();
}// end if

}// end for
}// end if
} catch(Exception e) {
e.printStackTrace();
}
}
}
<小时/>

以下是我遇到的异常..

java.lang.IllegalStateException: Folder is not Open
at com.sun.mail.pop3.POP3Folder.checkOpen(POP3Folder.java:551)
at com.sun.mail.pop3.POP3Folder.getProtocol(POP3Folder.java:581)
at com.sun.mail.pop3.POP3Message.loadHeaders(POP3Message.java:606)
at com.sun.mail.pop3.POP3Message.getHeader(POP3Message.java:383)
at javax.mail.internet.MimeMessage.getAddressHeader(MimeMessage.java:701)
at javax.mail.internet.MimeMessage.getRecipients(MimeMessage.java:534)
at servion.ForwardMail.main(ForwardMail.java:56)

如何消除这个异常???

最佳答案

您在处理完一条消息后关闭该文件夹,这意味着当您访问第二条消息的 Message 对象时该文件夹已关闭,因此 JavaMail 无法从服务器获取消息数据。

当您修复该问题时,请务必修复这些 common JavaMail mistakes .

最后,"smtp.starttls.enable" 属性应为"mail.smtp.starttls.enable"

关于java mail API转发邮件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43109136/

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