简体   繁体   中英

how to create an “offline” Java MimeMessage?

Java MimeMessage seems to require a Folder or Session for instantiation. Is there a sort of "offline" variety which allows creation of the message first, and then later it can be turned into a full-fledged MIME message? I'd like to stay within Java SE, but alternate API's would be ok if there's no such provision within SE.

If you create a message like so:

    Properties properties = new Properties();
    properties.put("mail.smtp.host", "smtp.example.com");
    properties.put("mail.smtp.port", "25");
    Session session = Session.getDefaultInstance(properties, null);
    MimeMessage m = new MimeMessage(session);

can the message object get sent to other methods or classes? Or will it just throw errors because the connection is bogus?

You can pass a null session to the MimeMessage constructor but you'll have to manage your own session and transport later on. See Creating a email message in Java without a mail session

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM