first working version of smtp class
This commit is contained in:
@@ -6,50 +6,69 @@ import org.json.simple.parser.ParseException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
public class SmtpClient {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SmtpClient.class);
|
||||
private static final String HOST = "mail.smtp.host";
|
||||
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 SSL = "mail.smtp.ssl.enable";
|
||||
private static final String UTF8 = "UTF-8";
|
||||
|
||||
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();
|
||||
props.put(HOST,host);
|
||||
props.put(PORT,port);
|
||||
props.put(SOCK,port);
|
||||
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() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username,password);
|
||||
}
|
||||
};
|
||||
|
||||
session = Session.getDefaultInstance(props,auth);
|
||||
LOG.debug("Created new session: {}", session);
|
||||
session = Session.getInstance(props);
|
||||
LOG.debug("Created new {}: {}", getClass().getSimpleName(),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 config = Files.readString(new File("/tmp/config.json").toPath());
|
||||
LOG.debug("config: {}",config);
|
||||
var json = parser.parse(config);
|
||||
LOG.debug("json: {}",json);
|
||||
JSONObject json = (JSONObject) parser.parse(config);
|
||||
|
||||
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