Here I hve seven pdf files in one folder which is saved in their Invoice No value. For example my pdf looks like below
Bil-to Customer No. is the Delear Code. Ive Connect to ms access db and able to fetch email id and Delear Code. This Code s differs in each pdf. Nw my task is to search this Delear Code in all pdf files and attach the corresponding email id. Db content s as follows
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
My email is as follows.
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....");