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

Email Adapter Support

The document details a Java program that retrieves email-related information from a database and sends emails with file attachments. The program accepts command line arguments specifying a functionality and date. It then establishes a database connection, retrieves sender, recipient, subject and SMTP details. Based on the functionality, it either sends query results as a CSV attachment or zips files from a date-specific directory and sends the ZIP attachment via email.

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

Email Adapter Support

The document details a Java program that retrieves email-related information from a database and sends emails with file attachments. The program accepts command line arguments specifying a functionality and date. It then establishes a database connection, retrieves sender, recipient, subject and SMTP details. Based on the functionality, it either sends query results as a CSV attachment or zips files from a date-specific directory and sends the ZIP attachment via email.

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.text.SimpleDateFormat;
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>
<date>");
return;
}

String functionality = args[0];


String date = 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);
} else if (functionality.equals("zip")) {
zipFilesAndSendAsAttachment(connection, sender, recipients, subject, smtpHost,
smtpPort, date);
} 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) {
// Hardcoded queries
String[] queries = {
"SELECT * FROM table1",
"SELECT * FROM table2"
};

// 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
date) {
// Get directory path based on the date provided
String directory = "/path/to/" + date;

// Create zip file


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

// Send email with zip attachment


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

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 directory, String zipFileName) {


// Implementation to zip files from the directory
// 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);

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy