Saving a template for a meeting-request

In my project, we usually don’t send mails directly, rather we create a mail file that the user downloads, opens using Outlook (and potentially modifies) and then sends themselves from their Outlook. This works for normal mails, my code for that looks like this:

		MailMessage msg = new MailMessage();
		MailAddressCollection recipient = new MailAddressCollection();
		recipient.add(emailDataSource.getTo());
		msg.setTo(recipient);
		msg.setSubject(emailDataSource.getSubject());
		msg.setHtmlBody(templateSource.getText());
		
		...
		
		File resultEmail = File.createTempFile("TEMPLATE_MAIL_", ".eml");
		resultEmail.deleteOnExit();
		FileOutputStream fs = new FileOutputStream(resultEmail);
		message.save(fs);
		fs.close();
		InputStream stream =  new FileInputStream(resultEmail);
		String contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(resultEmail.getPath());
		return new DefaultStreamedContent(stream, contentType, resultEmail.getPath());

This creates an email where things like the subject and recipients are already filled out, so the user can send it after opening it. Now I would like to do the same thing, but include a Meeting. I found that you can add an Appointmant to a mail like this:

Appointment app = new Appointment(
   "Location", // location of meeting
    startDate, // start date
    endDate, // end date
    new MailAddress("organizer@domain.com"), // organizer
    attendees); // attendees

msg.addAlternateView(app.requestApointment());

When I do it like that and open the resulting file, it displays the Appointment for the user that opening the file, with no way of sending it to the recipient specified in the code. How can I make it so that opening the file results in the user being presented the Outlook Meeting interface which enables the user to send the appointment to other people? Like this:
Screenshot 2023-04-20 102240.png (30.3 KB)

@KriseBeiKontron,

Could you convert the message to MapiMessage format for Outlook?
You can set MessageFlags to MapiMessageFlags.MSGFLAG_UNSENT

eml.addAlternateView(app.requestApointment());
MapiMessage msg = MapiMessage.fromMailMessage(eml);
msg.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
msg.save("test.msg");

Thank you for your response! Yes this seems to do what I want, when I open the mail with Outlook I see the Meeting interface. However, when I press “Send” I get an error with saying “You cannot send this item.” with no further information. Do you maybe know what could cause this?

@KriseBeiKontron,

You can use the MapiCalendar object:

java.util.Calendar c = java.util.Calendar.getInstance();
c.add(java.util.Calendar.HOUR, 1);
Date startDate = c.getTime();
c.add(java.util.Calendar.HOUR, 1);
Date endDate = c.getTime();

MapiRecipientCollection attendees = new MapiRecipientCollection();
attendees.add("attendee@domain.com", "SMTP", "Attendee", MapiRecipientType.MAPI_TO);
MapiCalendar cal = new MapiCalendar(
        "This is Location",
        "This is Summary",
        "This is Description",
        startDate,
        endDate,
        "organizer@domain.com",
        attendees);
MapiMessage msg = cal.convertToMapiMessage();
msg.setBodyContent("<b>Html content</b>", BodyContentType.Html);
msg.save("test.msg");