0% found this document useful (0 votes)
16 views4 pages

Final Code 1

The document contains code for a Java program that retrieves data from a database, generates CSV files from the results of SQL queries, zips the files, attaches the zip file to an email, and sends the email. The program takes a date as a parameter.

Uploaded by

vamshig555942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Final Code 1

The document contains code for a Java program that retrieves data from a database, generates CSV files from the results of SQL queries, zips the files, attaches the zip file to an email, and sends the email. The program takes a date as a parameter.

Uploaded by

vamshig555942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Please find attached the Adapter log files.

These logs contain valuable information


that may assist in troubleshooting any ongoing issues or provide insights into
recent activities and patches.

Pella Adapter Log and Configuration Files for Support Review

import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.zip.*;
import javax.mail.*;
import javax.mail.internet.*;

public class EmailSenderConcurrentProgram {


public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java EmailSenderConcurrentProgram <date>");
return;
}

String date = args[0];

Connection connection = null;


try {
// Establish database connection
connection =
DriverManager.getConnection("jdbc:oracle:thin:@your_database_host:1521:your_sid",
"your_db_username", "your_db_password");

// Retrieve email-related information from database


String sender = getEmailInfo(connection, "sender");
String recipients = getEmailInfo(connection, "recipients");
String subject = getEmailInfo(connection, "subject");
String smtpHost = getEmailInfo(connection, "smtp_host");
String smtpPort = getEmailInfo(connection, "smtp_port");

// Create individual CSV files for two hardcoded queries


String csvFileName1 = "/path/to/output1.csv";
String csvFileName2 = "/path/to/output2.csv";
createCsvFile(connection, query1, csvFileName1);
createCsvFile(connection, query2, csvFileName2);

// Get directory paths based on the date parameter and hardcoded path
String[] directories = {"/path/to/first/directory/" + date,
"/path/to/second/directory"};

// Create zip file


String zipFileName = "/path/to/output.zip";
createZipFile(directories, zipFileName, csvFileName1, csvFileName2);

// List all files included in the zip


String emailBody = listFilesInZip(zipFileName);

// Send email with zip attachment and file list in email body
sendEmailWithAttachment(sender, recipients, subject, smtpHost, smtpPort,
zipFileName, emailBody);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

public static String getEmailInfo(Connection connection, String infoType) throws


SQLException {
// Retrieve email-related information from database
String query = "SELECT " + infoType + " FROM email_info_table WHERE condition = ?";
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, "your_condition");
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getString(infoType);
}
}
}
return null;
}

public static void createCsvFile(Connection connection, String query, String


csvFileName) {
// Implementation to create CSV file from query results
try (PrintWriter writer = new PrintWriter(new FileWriter(csvFileName))) {
// Execute query and write results to CSV file
// This part is similar to the earlier examples
} catch (IOException | SQLException e) {
e.printStackTrace();
}
}

public static void createZipFile(String[] directories, String zipFileName, String


csvFileName1, String csvFileName2) {
// Implementation to zip files from multiple directories
try {
FileOutputStream fos = new FileOutputStream(zipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);

// Add CSV files to zip


addToZipFile(new File(csvFileName1), zos);
addToZipFile(new File(csvFileName2), zos);

for (String directory : directories) {


File dir = new File(directory);
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
addToZipFile(file, zos);
}
}
}
}

zos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void addToZipFile(File file, ZipOutputStream zos) throws IOException


{
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);

byte[] bytes = new byte[1024];


int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}

fis.close();
}

public static String listFilesInZip(String zipFileName) {


// Get list of files in the zip
StringBuilder body = new StringBuilder("List of files included in the attached zip
file:\n");
try (ZipFile zipFile = new ZipFile(zipFileName)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
body.append(entry.getName()).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return body.toString();
}

public static void sendEmailWithAttachment(String sender, String recipients, String


subject, String smtpHost, String smtpPort, String attachmentFileName, String
emailBody) {
// Configure mail properties
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mail.smtp.port", smtpPort);

// Create session
Session session = Session.getDefaultInstance(properties);

try {
// Create MimeMessage
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.setSubject(subject);
// Set recipients
String[] recipientList = recipients.split(",");
for (String recipient : recipientList) {
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(recipient.trim()));
}

// Create Multipart
Multipart multipart = new MimeMultipart();

// Attach file
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(new File(attachmentFileName));
multipart.addBodyPart(attachmentPart);

// Set email body


MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(emailBody);
multipart.addBodyPart(textPart);

// Set content
message.setContent(multipart);

// Send message
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException | IOException e) {
e.printStackTrace();
}
}
}

You might also like