SMTP server

This commit is contained in:
Daniel
2024-12-13 01:39:27 +02:00
parent 69d0bc0cf6
commit 96e9a09e79
6 changed files with 80 additions and 12 deletions

View File

@@ -1,5 +1,7 @@
package io.github.lumijiez;
import io.github.lumijiez.broker.BrokerConnector;
import io.github.lumijiez.ftp.FTPProducer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

View File

@@ -1,4 +1,4 @@
package io.github.lumijiez;
package io.github.lumijiez.broker;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
@@ -13,6 +13,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import io.github.lumijiez.Main;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

View File

@@ -1,7 +1,8 @@
package io.github.lumijiez;
package io.github.lumijiez.ftp;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.github.lumijiez.Main;
import org.apache.commons.net.ftp.FTPClient;
import java.io.ByteArrayInputStream;
@@ -18,7 +19,7 @@ public class FTPProducer {
String ftpPass = "symphony";
String ftpDir = "/";
FTPProducer() {
public FTPProducer() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override

View File

@@ -17,8 +17,8 @@
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>

View File

@@ -4,14 +4,27 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Main {
public static Logger logger = LogManager.getLogger(Main.class);
public static void main(String[] args) {
Logger logger = LogManager.getLogger(Main.class);
logger.info("SMTP server started");
try {
SMTPSender emailSender = new SMTPSender(
"smtp.gmail.com",
587,
"danthevip@gmail.com",
"",
false
);
String host = "smtp.gmail.com";
String port = "587";
String fromEmail = "your-email@gmail.com";
String fromPassword = "your-email-password";
String toEmail = "daniil.schipschi@isa.utm.md";
emailSender.sendEmail(
"danthevip@gmail.com",
"daniil.schipschi@isa.utm.md",
"Test",
"Test test test. Hehehehehehehehehehehehehehehehehehe!"
);
logger.info("Email sent successfully!");
} catch (Exception e) {
logger.error("Error sending email: {}", e.getMessage());
}
}
}

View File

@@ -0,0 +1,50 @@
package io.github.lumijiez;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SMTPSender {
private final String username;
private final String password;
private final String host;
private final int port;
private final boolean useSSL;
public SMTPSender(String host, int port, String username, String password, boolean useSSL) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.useSSL = useSSL;
}
public void sendEmail(String from, String to, String subject, String body) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
if (useSSL) {
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
} else {
props.put("mail.smtp.starttls.enable", "true");
}
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
}
}