我正在使用以下方法将已发送消息的副本保存在JavaMail中的用户"已发送"文件夹中。它适用于没有附件的电子邮件和附件小于1MB的电子邮件。但对于大于1MB的附件,程序在实际执行代码之前就停止了。知道怎么处理这个吗?
String host = ReadConfigPropertiesFile.getPropertyValue("server.host");
String smtpHost = ReadConfigPropertiesFile.getPropertyValue("smtp.host");
String from = "test@myserver.net";
String to = "test@myserver.net";
// Get system properties
Properties properties = System.getProperties();
// Setup smtp mail server
properties.setProperty(smtpHost, host);
properties.setProperty("mail.mime.encodeparameters", "true");
// properties.setProperty("mail.mime.decodeparameters","true");
properties.setProperty("mail.mime.encodefilename", "true");
// properties.setProperty("mail.mime.decodefilename","true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Peace ", "UTF-8");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message body
messageBodyPart.setContent("Hello attachment", "text/html; charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
String[] filename = {"C:/Users/Dake/Desktop/music.mp3"};
for (int i = 0; i < filename.length; i++) {
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename[i]);
messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName(filename[i]);
//messageBodyPart.setHeader("Content-Type", "text/plain; charset=UTF-8; name="+MimeUtility.encodeText(filename[i]));
// messageBodyPart.setHeader("Content-ID", filename[i]);
messageBodyPart.setFileName(MimeUtility.encodeText(filename[i], "UTF-8", null)); //encode filename
//bodyPart.setFileName(MimeUtility.encodeText(attachment.getName(), "UTF-8", null));
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message
message.setContent(multipart);
//set the time
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:MM:SS");
Date date = new Date();
String sentDate = df.format(date);
Date dd = (Date) df.parse(sentDate);
message.setSentDate(date);
// Send the message
Transport.send(message);
System.out.println("Message sent...");
// Copy message to "Sent Items" folder as read
Store store = session.getStore("imap");
store.connect(host, "user", "userpwd");
Folder folder = (Folder) store.getFolder("Sent");
if (!folder.exists()) {
folder.create(Folder.HOLDS_MESSAGES);
}
folder.open(Folder.READ_WRITE);
System.out.println("appending...");
// folder.appendMessages(new Message[]{message});
try {
folder.appendMessages(new Message[]{message});
// Message[] msgs = folder.getMessages();
message.setFlag(FLAGS.Flag.RECENT, true);
} catch (Exception ignore) {
System.out.println("error processing message " + ignore.getMessage());
} finally {
store.close();
// folder.close(false);
}
System.out.println("Msg send and saved ....");
}
当我运行上面的代码时,它会显示附加它永远停在那里。我使用ApacheJames服务器3.0beta4作为邮件服务器。
您的服务器是否因为发送要附加的消息花费了太长时间而中断了连接?(如果是,需要多长时间?)
或者服务器断开连接是因为它不允许您附加那么大的消息?
在调试输出中,您是否从服务器获得了任何有用的信息?
我知道这是一个老问题,但也许这对你或其他人有帮助。我想您的代码的问题在于您试图操纵Recent Flag:message.setFlag(FLAGS.Flag.RECENT, true);
。根据IMAP标准,客户端不能更改"最近使用的标志"。