SMTP server
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package io.github.lumijiez;
|
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.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package io.github.lumijiez;
|
package io.github.lumijiez.broker;
|
||||||
|
|
||||||
import com.rabbitmq.client.Channel;
|
import com.rabbitmq.client.Channel;
|
||||||
import com.rabbitmq.client.Connection;
|
import com.rabbitmq.client.Connection;
|
||||||
@@ -13,6 +13,8 @@ import java.util.concurrent.CountDownLatch;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import io.github.lumijiez.Main;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
package io.github.lumijiez;
|
package io.github.lumijiez.ftp;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import io.github.lumijiez.Main;
|
||||||
import org.apache.commons.net.ftp.FTPClient;
|
import org.apache.commons.net.ftp.FTPClient;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
@@ -18,7 +19,7 @@ public class FTPProducer {
|
|||||||
String ftpPass = "symphony";
|
String ftpPass = "symphony";
|
||||||
String ftpDir = "/";
|
String ftpDir = "/";
|
||||||
|
|
||||||
FTPProducer() {
|
public FTPProducer() {
|
||||||
Timer timer = new Timer();
|
Timer timer = new Timer();
|
||||||
timer.scheduleAtFixedRate(new TimerTask() {
|
timer.scheduleAtFixedRate(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
@@ -17,8 +17,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.sun.mail</groupId>
|
<groupId>com.sun.mail</groupId>
|
||||||
<artifactId>jakarta.mail</artifactId>
|
<artifactId>javax.mail</artifactId>
|
||||||
<version>2.0.1</version>
|
<version>1.6.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -4,14 +4,27 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
public static Logger logger = LogManager.getLogger(Main.class);
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Logger logger = LogManager.getLogger(Main.class);
|
try {
|
||||||
logger.info("SMTP server started");
|
SMTPSender emailSender = new SMTPSender(
|
||||||
|
"smtp.gmail.com",
|
||||||
|
587,
|
||||||
|
"danthevip@gmail.com",
|
||||||
|
"",
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
String host = "smtp.gmail.com";
|
emailSender.sendEmail(
|
||||||
String port = "587";
|
"danthevip@gmail.com",
|
||||||
String fromEmail = "your-email@gmail.com";
|
"daniil.schipschi@isa.utm.md",
|
||||||
String fromPassword = "your-email-password";
|
"Test",
|
||||||
String toEmail = "daniil.schipschi@isa.utm.md";
|
"Test test test. Hehehehehehehehehehehehehehehehehehe!"
|
||||||
|
);
|
||||||
|
|
||||||
|
logger.info("Email sent successfully!");
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error sending email: {}", e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user