SMTP server
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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