import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FTPUploadExample {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your_username";
String password = "your_password";
String localFile = "local_file_path";
String remoteFile = "remote_file_path";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FileInputStream inputStream = new FileInputStream(localFile);
boolean uploaded = ftpClient.storeFile(remoteFile, inputStream);
inputStream.close();
if (uploaded) {
System.out.println("File uploaded successfully.");
} else {
System.out.println("File upload failed.");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import org.apache.commons.net.smtp.SMTPClient;
public class EmailSender {
public static void main(String[] args) {
String smtpServer = "smtp.example.com";
int port = 25;
String sender = "sender@example.com";
String recipient = "recipient@example.com";
String subject = "Test Email";
String message = "This is a test email.";
SMTPClient smtpClient = new SMTPClient();
try {
smtpClient.connect(smtpServer, port);
smtpClient.login();
smtpClient.sendSimpleMessage(sender, recipient, subject, message);
smtpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (smtpClient.isConnected()) {
smtpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}