gpt4 book ai didi

java - 如何在 pdf 文件内容中搜索并附加到电子邮件

转载 作者:行者123 更新时间:2023-12-01 11:13:31 26 4
gpt4 key购买 nike

这里,我在一个文件夹中有七个 pdf 文件,该文件保存在其“发票编号”中。例如,我的 pdf 如下所示 enter image description here

Bil-to 客户编号是 Delear 代码。我已经连接到 ms access 数据库并能够获取电子邮件 ID 和 Delear 代码。每个 pdf 中的本准则均有所不同。我的任务是在所有 pdf 文件中搜索这个 Delear 代码并附上相应的电子邮件 ID。 Db内容如下

STE002 a@gmail.com
C04004 a@gmail.com
RS0002 b@gmail.com
RS0006 b@gmail.com
RS0009 c@gmail.com
RS0001 c@gmail.com
C01020 d@gmail.com

我的电子邮件如下。

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:PDF1";
Connection con = DriverManager.getConnection(url);
java.sql.Statement st = con.createStatement();

String sql = "SELECT * FROM new"; // Retrieve data from Person table in database
ResultSet rs = st.executeQuery(sql);

while(rs.next()){


String code = rs.getString("Dealer Code");
String email = rs.getString("Dealer Email ID");

System.out.println(+ code + " " + email);

//email


String to = email;

String from = "abcd.gmail.com";

final String username = "abcd.gmail.com";//change accordingly
final String password = "*******";//change accordingly

// Assuming you are sending email through relay.jangosmtp.net
String host = "smtp.gmail.com";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");

// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));

// Set Subject: header field
message.setSubject("Testing Subject");

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Now set the actual message
messageBodyPart.setText("This is message body");

// Create a multipar message
Multipart multipart = new MimeMultipart();

// Set text message part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();

String filename = "E:\\Sales.pdf";

DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

// Send the complete message parts
message.setContent(multipart);

// Send message
Transport.send(message);

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

最佳答案

尝试使用Apache PDFBox这是text extraction tutorial

要查找 pdf 文件,请使用 listFiles(FileFilter filter) 以下是此方法的示例:

private static String directoryPath = "/Users/aal/Documents";
private static String extension = "pdf";

public static void main(String[] args) {

File file = null;
File[] paths;

try {
file = new File(directoryPath);

FileFilter fileFilter = new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(extension);
}
};

// returns pathnames for files and directory
paths = file.listFiles(fileFilter);

for (File path : paths) {
// prints file and directory paths
System.out.println(path);
}
} catch (Exception e) {
e.printStackTrace();
}

}

关于java - 如何在 pdf 文件内容中搜索并附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32112141/

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