Skip to content

Instantly share code, notes, and snippets.

@Kawzeg
Last active April 20, 2022 12:24
Show Gist options
  • Save Kawzeg/69a4de7b1e39119a6d7bb6bf43b3876c to your computer and use it in GitHub Desktop.
Save Kawzeg/69a4de7b1e39119a6d7bb6bf43b3876c to your computer and use it in GitHub Desktop.
Create .eml files with javax.mail
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Creates .eml files
*/
public class Pat {
public static void main(String[] args) throws MessagingException, IOException {
Message msg = createMessage("[email protected]", "[email protected]", "This is Seriöus Business! \uD83D\uDE10", "I ❤ \uD83C\uDF55");
File outputFile = new File("draft.eml");
try (FileOutputStream out = new FileOutputStream(outputFile)) {
msg.writeTo(out);
}
Desktop.getDesktop().open(outputFile);
}
public static Message createMessage(String from, String to, String subject, String content) throws MessagingException {
Message msg = new MimeMessage((Session)null);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
MimeBodyPart body = new MimeBodyPart();
body.setText(content);
msg.setContent(new MimeMultipart(body));
return msg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment