Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created July 11, 2020 21:54
Show Gist options
  • Save maxandersen/df975a9ee0002592ae94eb2ba7b485af to your computer and use it in GitHub Desktop.
Save maxandersen/df975a9ee0002592ae94eb2ba7b485af to your computer and use it in GitHub Desktop.
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.2.0
//DEPS org.jodd:jodd-mail:5.1.5
import jodd.mail.EmailFilter;
import jodd.mail.ImapServer;
import jodd.mail.MailServer;
import jodd.mail.ReceiveMailSession;
import jodd.mail.ReceivedEmail;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import java.util.concurrent.Callable;
import static java.lang.System.out;
@Command(name = "gruymail", mixinStandardHelpOptions = true, version = "gruymail 0.1",
description = "gruymail made with jbang")
class gruymail implements Callable<Integer> {
@CommandLine.Option(names={ "--host"}, defaultValue = "imap.googlemail.com", required = true)
private String host;
@CommandLine.Option(names={ "--user"}, required = true)
private String user;
@CommandLine.Option(names={"--debug"})
boolean debug;
@CommandLine.Option(names={"--password"}, required = true)
private String password;
public static void main(String... args) {
int exitCode = new CommandLine(new gruymail()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception { // your business logic goes here...
out.println("Login...");
ImapServer imapServer = MailServer.create()
.host(host)
.ssl(true)
.auth(user, password)
.debugMode(debug)
.buildImapMailServer();
out.println("Open...");
ReceiveMailSession session = imapServer.createSession();
session.open();
EmailFilter anylistid = EmailFilter.filter()
.header("List-Id", "*");
out.println("Search...");
var mails = session.receive().envelopeOnly()
.filter(anylistid)
.get();
out.println("Found " + mails.length);
for (ReceivedEmail mail:mails
) {
out.println(mail.header("List-Id"));
}
session.close();
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment