Last active
April 20, 2022 12:24
-
-
Save Kawzeg/69a4de7b1e39119a6d7bb6bf43b3876c to your computer and use it in GitHub Desktop.
Create .eml files with javax.mail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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