B4J Question SMTP Calendar invite

prajinpraveen

Active Member
Licensed User
Longtime User
Hello All,

Is it possible to send out calendar invite using SMTP or any Alternate library.
I have tried with inline Java, it works. wondering if there is any alternate solution

thank you

#AdditionalJar: javax.mail-1.6.2.jar
#AdditionalJar: activation.jar
#AdditionalJar: additionnal.jar

#VirtualMachineArgs: --add-opens java.desktop/sun.awt.windows=ALL-UNNAMED --add-opens java.desktop/sun.awt.datatransfer=ALL-UNNAMED

Java Code:
#if java
import java.io.*;
import java.util.zip.*;
import java.util.List;
import java.util.ArrayList;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import java.util.Properties;
import javax.mail.*;
import javax.mail.Message;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.AddressException;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.activation.MailcapCommandMap;


import javax.mail.util.ByteArrayDataSource;



/**
     * method to send mail request along with calendar intergration for interview
     * @author simbu
     * @param emails
     * @param string
     * @param subject
     * @throws Exception
     */
    public void eventRequest(List<String> emails, String string, String subject) throws Exception {
        final MimetypesFileTypeMap mimetypes = (MimetypesFileTypeMap) MimetypesFileTypeMap.getDefaultFileTypeMap();
        mimetypes.addMimeTypes("text/calendar ics ICS");
        final MailcapCommandMap mailcap = (MailcapCommandMap) MailcapCommandMap.getDefaultCommandMap();
        mailcap.addMailcap("text/calendar;; x-java-content-handler=com.sun.mail.handlers.text_plain");
        // Original answer from Som:
        // MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
        mailcap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
        mailcap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
        mailcap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
        mailcap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
        mailcap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");

        // Additional elements to make DSN work
        mailcap.addMailcap("multipart/report;;  x-java-content-handler=com.sun.mail.dsn.multipart_report");
        mailcap.addMailcap("message/delivery-status;; x-java-content-handler=com.sun.mail.dsn.message_deliverystatus");
        mailcap.addMailcap("message/disposition-notification;; x-java-content-handler=com.sun.mail.dsn.message_dispositionnotification");
        mailcap.addMailcap("text/rfc822-headers;;   x-java-content-handler=com.sun.mail.dsn.text_rfc822headers");
       
       

        // **************//
        // Provide SMTP Details
        final String username = "username"; // your username
        final String password = "password"; // your password

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.startTLS.enable", "true");
        props.put("mail.smtp.host", "mail.XXX.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.ssl.trust", "mail.XXX.com");

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        // ************//

        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(username));
        } catch (AddressException e) {
            e.printStackTrace();
            // log.error(e);
            System.out.println(e);
        } catch (MessagingException e) {
            e.printStackTrace();
            // log.error(e);
            System.out.println(e);
        }
        message.setSubject(subject);    // Subject of the event
       
        //adding emails to internetaddres
        for (String email : emails) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        }

        Multipart multipart = new MimeMultipart("alternative");

        BodyPart messageBodyPart = buildCalendarPart(string);
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);

        // send the message
        Transport transport = session.getTransport("smtp");
        transport.connect();
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    /**
     * @author simbu
     * @return description part along with design
     * @throws MessagingException
     */
    private BodyPart buildHtmlTextPart() throws MessagingException {

        MimeBodyPart descriptionPart = new MimeBodyPart();

        // Note: even if the content is specified as being text/html, outlook
        // won't read correctly tables at all
        // and only some properties from div:s. Thus, try to avoid too fancy
        // content
        String content = "<h1 >simple meeting invitation</h1>";
        descriptionPart.setContent(content, "text/html; charset=utf-8");

        return descriptionPart;
    }

    /**
     * @author simbu
     * @param bodyMsg
     * @return calendardata along with design
     * @throws Exception
     */
    private BodyPart buildCalendarPart(String bodyMsg) throws Exception {

        BodyPart calendarPart = new MimeBodyPart();

        // TODO: DATE AND TIME OF EVENT
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        Date start = cal.getTime();
        cal.add(Calendar.HOUR_OF_DAY, 3);
        final Date end = cal.getTime();

        // check the icalendar spec in order to build a more complicated meeting
        SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");
       
        // request
        final String calendarContent = "BEGIN:VCALENDAR\n" + "METHOD:REQUEST\n" + "PRODID: BCP - Meeting\n" +  "VERSION:2.0\n" + "BEGIN:VEVENT\n" + "DTSTAMP:" + iCalendarDateFormat.format(start) + "\n" + "DTSTART:" + iCalendarDateFormat.format(start) + "\n" + "DTEND:" + iCalendarDateFormat.format(end) + "\n" + "SUMMARY:Interview with candidate1\n" + "UID:324\n" + "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=FALSE:MAILTO:rajeshwright@gmail.com\n" + "ORGANIZER:MAILTO:rajeshwright@gmail.com\n"
                + "LOCATION:Company\n" + "DESCRIPTION:" + bodyMsg + "\n" + "SEQUENCE:0\n" + "PRIORITY:5\n" + "CLASS:PUBLIC\n" + "STATUS:CONFIRMED\n" + "TRANSP:OPAQUE\n" + "BEGIN:VALARM\n" + "ACTION:DISPLAY\n" + "DESCRIPTION:REMINDER\n" + "END:VALARM\n" + "TRIGGER;RELATED=START:-PT00H15M00S\n" + "END:VEVENT\n" + "END:VCALENDAR";

        calendarPart.addHeader("Content-Class", "urn:content-classes:calendarmessage");
        //calendarPart.setContent(calendarContent, "text/calendar;method=CANCEL");
        calendarPart.setContent(calendarContent, "text/calendar;method=REQUEST");

        return calendarPart;
    }
 
Top