【发布时间】:2015-11-13 17:38:47
【问题描述】:
在这里,我在一个文件夹中保存了七个 pdf 文件,这些文件保存在他们的 Invoice No 值中。例如,我的 pdf 如下所示
Bil-to 客户编号是 Delar 代码。我连接到 ms 访问数据库并能够获取电子邮件 ID 和 Delear 代码。此代码在每个 pdf 中有所不同。 Nw 我的任务是在所有 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....");
【问题讨论】: