Java连接到邮箱把邮件信息以及附件存放在数据库中
时间: 2023-05-20 07:05:22 浏览: 126
可以使用JavaMail API连接到邮箱,获取邮件信息和附件,然后使用JDBC将它们存储到数据库中。以下是一个简单的示例代码:
```java
import java.sql.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailToDatabase {
public static void main(String[] args) {
String host = "imap.gmail.com";
String username = "your_email@gmail.com";
String password = "your_password";
String databaseUrl = "jdbc:mysql://localhost:3306/mydatabase";
String databaseUser = "root";
String databasePassword = "root";
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.host", host);
props.setProperty("mail.imaps.port", "993");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect(username, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
for (Message message : messages) {
String subject = message.getSubject();
String from = InternetAddress.toString(message.getFrom());
String to = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
String cc = InternetAddress.toString(message.getRecipients(Message.RecipientType.CC));
String bcc = InternetAddress.toString(message.getRecipients(Message.RecipientType.BCC));
String sentDate = message.getSentDate().toString();
String receivedDate = message.getReceivedDate().toString();
String contentType = message.getContentType();
// Save message metadata to database
Connection conn = DriverManager.getConnection(databaseUrl, databaseUser, databasePassword);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO messages (subject, from, to, cc, bcc, sent_date, received_date, content_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
stmt.setString(1, subject);
stmt.setString(2, from);
stmt.setString(3, to);
stmt.setString(4, cc);
stmt.setString(5, bcc);
stmt.setString(6, sentDate);
stmt.setString(7, receivedDate);
stmt.setString(8, contentType);
stmt.executeUpdate();
stmt.close();
conn.close();
// Save message attachments to database
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&
!StringUtils.isNotBlank(bodyPart.getFileName())) {
continue; // Skip non-attachment parts
}
// Save attachment to database
InputStream is = bodyPart.getInputStream();
conn = DriverManager.getConnection(databaseUrl, databaseUser, databasePassword);
stmt = conn.prepareStatement("INSERT INTO attachments (message_id, filename, content) VALUES (?, ?, ?)");
stmt.setInt(1, message.getMessageNumber());
stmt.setString(2, bodyPart.getFileName());
stmt.setBinaryStream(3, is, is.available());
stmt.executeUpdate();
stmt.close();
conn.close();
}
}
inbox.close(false);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请注意,此代码仅用于演示目的,实际应用中需要添加错误处理和安全性检查。
阅读全文