Java-basierte Mailinglisten-Anwendung, die auf IMAP+SMTP aufsetzt, und damit (fast) jede Mailbox in eine Mailingliste verwandeln kann.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

138 lines
4.9 KiB

package de.srsoftware.widerhall.mail;
import com.sun.mail.imap.protocol.ENVELOPE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import static de.srsoftware.widerhall.Util.t;
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 AUTH = "mail.smtp.auth";
private static final String ENVELOPE_FROM = "mail.smtp.from";
private static final String SSL = "mail.smtp.ssl.enable";
private static final String UTF8 = "UTF-8";
private final String host,password,username,from;
private final int port;
private boolean forwardUsingListAddress = true;
private Session session;
public SmtpClient(String host, int port, String username, String password, String envelopeFrom){
this.username = username;
this.password = password;
this.host = host;
this.port = port;
this.from = envelopeFrom;
}
public void forward(String newSender, List<String> receivers, Message message, String subject, boolean forwardAsAttachment, boolean bcc, String replyTo) throws MessagingException {
if (session == null) login();
MimeMessage forward = new MimeMessage(session);
var oldSender = message.getFrom()[0].toString();
{ // drop old (von ... comment)
var pos = subject.indexOf(" (von ");
while (pos > 0){
var end = subject.indexOf(')',pos);
if (end < pos) break;
subject = (subject.substring(0,pos)+subject.substring(end+1)).trim();
pos = subject.indexOf(" (von ");
}
}
if (newSender != null){
forward.setFrom(newSender);
forward.setSubject(subject+" (von "+oldSender+")");
} else {
forward.setFrom(oldSender);
forward.setSubject(subject);
}
if (replyTo != null) forward.setReplyTo(InternetAddress.parse(replyTo));
var recipientType = bcc ? Message.RecipientType.BCC : Message.RecipientType.TO;
forward.setRecipients(recipientType,InternetAddress.parse(String.join(", ",receivers)));
MimeMultipart multipart = new MimeMultipart();
if (forwardAsAttachment){
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setText(t("Find the forwarded message in the attachment(s)!\n"));
multipart.addBodyPart(bodyPart);
// create another body part to contain the message to be forwarded
bodyPart = new MimeBodyPart();
// forwardedMsg is the MimeMessage object you want to forward as an attachment
bodyPart.setContent(message, "message/rfc822");
bodyPart.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(bodyPart);
} else {
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(message.getDataHandler());
multipart.addBodyPart(bodyPart);
}
forward.setContent(multipart);
send(forward);
}
public String host() {
return host;
}
public SmtpClient login(){
if (session == null) {
Properties props = new Properties();
props.put(HOST, host);
props.put(PORT, port);
props.put(AUTH, true);
props.put(SSL, true);
props.put(ENVELOPE_FROM,from);
session = Session.getInstance(props);
LOG.debug("Created new session: {}", session);
}
return this;
}
public String password() {
return password;
}
public int port() {
return port;
}
public void send(Message message) throws MessagingException {
LOG.debug("sending mail…");
Transport.send(message,username,password);
LOG.debug("…sent");
}
public void send(String senderAdress, String senderName, String receivers, String subject, String content) throws MessagingException, UnsupportedEncodingException {
login();
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));
send(message);
}
public String username() {
return username;
}
}