first working version of smtp class
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
|
.idea
|
||||||
target
|
target
|
||||||
|
|||||||
4
pom.xml
4
pom.xml
@@ -15,8 +15,8 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.mail</groupId>
|
<groupId>com.sun.mail</groupId>
|
||||||
<artifactId>javax.mail-api</artifactId>
|
<artifactId>javax.mail</artifactId>
|
||||||
<version>1.6.2</version>
|
<version>1.6.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|||||||
@@ -6,50 +6,69 @@ import org.json.simple.parser.ParseException;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.json.stream.JsonParser;
|
import javax.mail.*;
|
||||||
import javax.mail.Authenticator;
|
import javax.mail.internet.InternetAddress;
|
||||||
import javax.mail.PasswordAuthentication;
|
import javax.mail.internet.MimeMessage;
|
||||||
import javax.mail.Session;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class SmtpClient {
|
public class SmtpClient {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(SmtpClient.class);
|
private static final Logger LOG = LoggerFactory.getLogger(SmtpClient.class);
|
||||||
private static final String HOST = "mail.smtp.host";
|
private static final String HOST = "mail.smtp.host";
|
||||||
private static final String PORT = "mail.smtp.port";
|
private static final String PORT = "mail.smtp.port";
|
||||||
private static final String SOCK = "mail.smtp.socketFactory.port";
|
|
||||||
private static final String CLASS = "mail.smtp.socketFactory.class";
|
|
||||||
private static final String SOCKET_FACTORY = javax.net.ssl.SSLSocketFactory.class.getCanonicalName();
|
|
||||||
private static final String AUTH = "mail.smtp.auth";
|
private static final String AUTH = "mail.smtp.auth";
|
||||||
|
private static final String SSL = "mail.smtp.ssl.enable";
|
||||||
|
private static final String UTF8 = "UTF-8";
|
||||||
|
|
||||||
private Session session;
|
private Session session;
|
||||||
|
|
||||||
public void login(String host, int port, String username, String password){
|
public SmtpClient(Map<String,Object> config){
|
||||||
|
String host = (String) config.get("host");
|
||||||
|
long port = (long) config.get("port");
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
props.put(HOST,host);
|
props.put(HOST,host);
|
||||||
props.put(PORT,port);
|
props.put(PORT,port);
|
||||||
props.put(SOCK,port);
|
|
||||||
props.put(AUTH,true);
|
props.put(AUTH,true);
|
||||||
props.put(CLASS,SOCKET_FACTORY);
|
props.put(SSL,true);
|
||||||
|
// props.put("mail.smtp.localhost","mail.keawe.de");
|
||||||
|
|
||||||
Authenticator auth = new Authenticator() {
|
session = Session.getInstance(props);
|
||||||
@Override
|
LOG.debug("Created new {}: {}", getClass().getSimpleName(),session);
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
|
||||||
return new PasswordAuthentication(username,password);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
session = Session.getDefaultInstance(props,auth);
|
|
||||||
LOG.debug("Created new session: {}", session);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, ParseException {
|
public void send(JSONObject config, String senderAdress, String senderName, String receivers, String subject, String content) throws MessagingException, UnsupportedEncodingException {
|
||||||
|
MimeMessage message = new MimeMessage(session);
|
||||||
|
message.addHeader("Content-Type","text/plain; charset="+UTF8);
|
||||||
|
message.addHeader("format","flowed");
|
||||||
|
message.addHeader("Content-Transfer-Encoding","8bit");
|
||||||
|
|
||||||
|
message.setFrom(new InternetAddress(senderAdress,senderName));
|
||||||
|
message.setReplyTo(InternetAddress.parse(senderAdress,false));
|
||||||
|
message.setSubject(subject,UTF8);
|
||||||
|
message.setText(content,UTF8);
|
||||||
|
message.setSentDate(new Date());
|
||||||
|
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receivers,false));
|
||||||
|
|
||||||
|
String username = (String) config.get("user");
|
||||||
|
String password = (String) config.get("password");
|
||||||
|
|
||||||
|
LOG.debug("Versende Mail…");
|
||||||
|
Transport.send(message,username,password);
|
||||||
|
LOG.debug("…versendet");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException, ParseException, MessagingException {
|
||||||
var parser = new JSONParser();
|
var parser = new JSONParser();
|
||||||
var config = Files.readString(new File("/tmp/config.json").toPath());
|
var config = Files.readString(new File("/tmp/config.json").toPath());
|
||||||
LOG.debug("config: {}",config);
|
JSONObject json = (JSONObject) parser.parse(config);
|
||||||
var json = parser.parse(config);
|
|
||||||
LOG.debug("json: {}",json);
|
String testSender = (String) json.get("sender");
|
||||||
|
String testReceiver = (String) json.get("receiver");
|
||||||
|
new SmtpClient(json).send(json,testSender,"Stephan Richter",testReceiver,"Test","Dies ist ein Test");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user