Merge branch 'main' into lang_de

This commit is contained in:
2022-04-20 17:47:59 +02:00
18 changed files with 695 additions and 91 deletions

View File

@@ -23,6 +23,7 @@ public class ImapClient {
private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class);
private HashSet<MessageHandler> listeners = new HashSet<>();
private boolean stopped = false;
private Session session;
public ListeningThread addListener(MessageHandler messageHandler) {
listeners.add(messageHandler);
@@ -53,7 +54,7 @@ public class ImapClient {
private void openInbox() throws MessagingException {
LOG.debug("Verbinden und Einloggen…");
Properties props = imapProps();
Session session = Session.getInstance(props);
session = Session.getInstance(props);
Store store = session.getStore(Constants.IMAPS);
store.connect(host,username,password);
LOG.debug("Verbunden. Öffne {}:",folderName);
@@ -75,6 +76,8 @@ public class ImapClient {
for (Message message : inbox.getMessages()){
if (message.isSet(Flags.Flag.SEEN)) continue;
handle(message);
Folder folder = message.getFolder();
if (!folder.isOpen()) folder.open(Folder.READ_WRITE);
message.setFlag(Flags.Flag.SEEN,true);
}
}
@@ -158,6 +161,21 @@ public class ImapClient {
return folderName;
}
public ImapClient move(Message message, String destinationFolder) throws MessagingException {
if (listeningThread == null || listeningThread.stopped) throw new IllegalStateException("IMAP client not connected!");
var source = message.getFolder();
if (!source.isOpen()) source.open(Folder.READ_WRITE);
var messages = new Message[]{message};
var store = source.getStore();
var dest = store.getFolder(new URLName(destinationFolder));
if (!dest.exists()) dest.create(Folder.HOLDS_MESSAGES);
source.copyMessages(messages,dest);
source.setFlags(messages, new Flags(Flags.Flag.DELETED), true);
source.close(true);
return this;
}
public ImapClient start() {
stop();

View File

@@ -30,21 +30,49 @@ public class SmtpClient {
this.port = port;
}
public void bccForward(String from, Message message, List<String> emails) throws MessagingException {
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);
forward.setFrom(from);
forward.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(String.join(", ",emails)));
forward.setSubject(message.getSubject());
var oldSender = message.getFrom()[0].toString();
if (newSender != null){
var pos = subject.indexOf(" (from ");
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(" (from ");
}
forward.setFrom(newSender);
forward.setSubject(subject+" (from "+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();
MimeBodyPart messageBodyPart = new MimeBodyPart();
if (forwardAsAttachment){
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setText("Find the forwarded message in the attachment(s)!\n");
multipart.addBodyPart(bodyPart);
messageBodyPart.setDataHandler(message.getDataHandler());
multipart.addBodyPart(messageBodyPart);
// 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);
}
@@ -64,6 +92,7 @@ public class SmtpClient {
}
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");