0% found this document useful (0 votes)
13 views

Email Files Code

This Java program retrieves email-related information from a database and sends emails with attachments based on the functionality and parameter passed in. It establishes a database connection, retrieves sender, recipient, subject and other email configuration details. Based on the functionality, it either executes SQL queries and sends the output as a CSV attachment, or zips files from multiple directories and sends the zip file as an attachment. It uses Java mail API to send the emails.

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)
13 views

Email Files Code

This Java program retrieves email-related information from a database and sends emails with attachments based on the functionality and parameter passed in. It establishes a database connection, retrieves sender, recipient, subject and other email configuration details. Based on the functionality, it either executes SQL queries and sends the output as a CSV attachment, or zips files from multiple directories and sends the zip file as an attachment. It uses Java mail API to send the emails.

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/ 3

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 != 2) {
System.out.println("Usage: java EmailSenderConcurrentProgram <functionality>
<parameter>");
return;
}

String functionality = args[0];


String parameter = args[1];

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");

if (functionality.equals("queries")) {
sendQueriesAsCsvAttachment(connection, sender, recipients, subject, smtpHost,
smtpPort, parameter);
} else if (functionality.equals("zip")) {
zipFilesAndSendAsAttachment(connection, sender, recipients, subject, smtpHost,
smtpPort, parameter);
} else {
System.out.println("Invalid functionality.");
}
} 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 sendQueriesAsCsvAttachment(Connection connection, String sender,


String recipients, String subject, String smtpHost, String smtpPort, String
parameter) {
// Queries to execute based on the parameter
String[] queries = getQueries(parameter);

// Create CSV file


String csvFileName = "/path/to/output.csv";
createCsvFile(connection, queries, csvFileName);

// Send email with CSV attachment


sendEmailWithAttachment(sender, recipients, subject, smtpHost, smtpPort,
csvFileName);
}

public static void zipFilesAndSendAsAttachment(Connection connection, String


sender, String recipients, String subject, String smtpHost, String smtpPort, String
parameter) {
// Directory paths to zip based on the parameter
String[] directories = getDirectories(parameter);

// Create zip file


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

// Send email with zip attachment


sendEmailWithAttachment(sender, recipients, subject, smtpHost, smtpPort,
zipFileName);
}

public static String[] getQueries(String parameter) {


// Logic to determine queries based on the parameter
// Example: return different queries based on the value of parameter
return new String[] {
"SELECT * FROM table1 WHERE column = '" + parameter + "'",
"SELECT * FROM table2 WHERE column = '" + parameter + "'"
};
}

public static String[] getDirectories(String parameter) {


// Logic to determine directories based on the parameter
// Example: return different directories based on the value of parameter
return new String[] {
"/path/to/directory1/" + parameter,
"/path/to/directory2/" + parameter
};
}

public static void createCsvFile(Connection connection, String[] queries, String


csvFileName) {
// Implementation to create CSV file from query results
// This part is similar to the earlier example
}

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


// Implementation to zip files from multiple directories
// This part is similar to the earlier example
}

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


subject, String smtpHost, String smtpPort, String attachmentFileName) {
// 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 content
message.setContent(multipart);

You might also like