Search This Blog

Showing posts with label java mail. Show all posts
Showing posts with label java mail. Show all posts

Wednesday, April 16, 2014

How to Send Mail to Outlook i Calender Meeting Remainder ( ICS )


//use below sample and enjoy, which i taken from another site and modify with my choice..
//hope this will help you a lot and give me a copy of yours if u done any modification..

package com.test;

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

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailRemainder
{
    public BodyPart buildHtmlTextPart () throws MessagingException
    {

        MimeBodyPart descriptionPart = new MimeBodyPart ();

        // Note: even if the content is spcified 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 = "Sample Body Content";
        descriptionPart.setContent ( content, "text/html; charset=utf-8" );

        return descriptionPart;
    }

    // define somewhere the icalendar date format
    private static SimpleDateFormat    iCalendarDateFormat    = new SimpleDateFormat ( "yyyyMMdd'T'HHmm'00'" );

    public BodyPart buildCalendarPart (String fromMailId, String location, String description ) throws Exception
    {

        BodyPart calendarPart = new MimeBodyPart ();

        //System.out.println ( TimeZone.getTimeZone ( "UTC" ) );
        Calendar cal = Calendar.getInstance ();   //TimeZone.getDefault () //TimeZone.getTimeZone ( "UTC" )
        //cal.add ( Calendar.DAY_OF_MONTH, 1 );
        cal.add ( Calendar.MINUTE, 10 );
        Date start = cal.getTime ();
        System.out.println ( "Meeting Start Time : " + start );
       
        //cal.add ( Calendar.HOUR_OF_DAY, 3 );
        cal.add ( Calendar.MINUTE, 15 );
        Date end = cal.getTime ();
        System.out.println ( "Meeting End Time : " + end );
       
        // check the icalendar spec in order to build a more complicated meeting request
        String calendarContent =
                "BEGIN:VCALENDAR\n"
               
                    + "METHOD:REQUEST\n"
                    + "PRODID:-//Sdex Portal//NONSGML Sdex//EN\n"
                    + "VERSION:2.0\n"
               
                    + "BEGIN:VTIMEZONE\n"
                    + "TZID:India Standard Time\n"
                        + "BEGIN:STANDARD\n"
                        + "DTSTART:" + iCalendarDateFormat.format ( start ) + "\n"
                        + "TZOFFSETFROM:+0530\n"
                        + "TZOFFSETTO:+0530\n"
                        + "END:STANDARD\n"
                    + "END:VTIMEZONE\n"
                   
                    + "BEGIN:VEVENT\n"
                    + "DTSTAMP:" + iCalendarDateFormat.format ( start ) + "\n"
                    + "DTSTART:" + iCalendarDateFormat.format ( start ) + "\n"
                    + "DTEND:" + iCalendarDateFormat.format ( end ) + "\n"
                    + "SUMMARY:test request\n"
                    + "UID:" + Math.random () +"\n"
                    + "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:"+ fromMailId +"\n"
                    + "ORGANIZER:MAILTO:"+ fromMailId +"\n"
                    + "LOCATION:"+ location +"\n"
                    + "DESCRIPTION:"+ description +"\n"
                    + "SEQUENCE:1\n"
                    + "PRIORITY:1\n"
                    + "CLASS:PUBLIC\n"
                    + "STATUS:CONFIRMED\n"
                    + "TRANSP:OPAQUE\n"
                        + "BEGIN:VALARM\n"
                        + "ACTION:DISPLAY\n"
                        + "DESCRIPTION:REMINDER\n"
                        + "TRIGGER;RELATED=START:-PT00H15M00S\n"
                        + "END:VALARM\n"
                    + "END:VEVENT\n"
                   
                + "END:VCALENDAR";

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

        return calendarPart;
    }

    public static void main ( String args[] ) throws Exception
    {
        String fromMailId = "vijay@XXXXXXX.com";
        String toMailId = "vkumar@XXXXXXX.com";
        final String userName = "vijay@XXXXXXX.com";
        final String password = "test123";
        String protocol = "smtp";
        int port  = 25;
       
        Session session = null;
        // Security.addProvider ( new com.sun.net.ssl.internal.ssl.Provider () ); //this will use for SSL/HTTPS
        Properties props = new Properties ();
        props.setProperty ( "mail.transport.protocol", protocol );
        props.setProperty ( "mail.host", "smtp.yourserver.com" );
        props.put ( "mail.debug", "false" );
        props.put ( "mail.smtp.port", port );
        if ( Boolean.parseBoolean ( "true" ) )
        {
            props.put ( "mail.smtp.auth", "true" );
            props.put ( "mail.smtp.socketFactory.port", port );
        }
        if ( Boolean.parseBoolean ( "false" ) )
        {
            props.put ( "mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory" );
            props.put ( "mail.smtp.socketFactory.fallback", "false" );
        }
        if ( Boolean.parseBoolean ( "true" ) )
        {
            session = Session.getDefaultInstance ( props, new javax.mail.Authenticator () {
                // @Override
                protected PasswordAuthentication getPasswordAuthentication ()
                {
                    return new PasswordAuthentication ( userName, password );
                }
            } );
        }
        else
        {
            session = Session.getInstance ( props );
        }

        // session.setDebug ( true );

        EmailRemainder emailRemainder = new EmailRemainder ();
        MimeMessage message = new MimeMessage ( session );
        message.setFrom ( new InternetAddress ( fromMailId ) );
        message.setSubject ( "Subject: Hi 2" );
        message.addRecipient ( Message.RecipientType.TO, new InternetAddress ( toMailId ) );
        // Create an alternative Multipart
        Multipart multipart = new MimeMultipart ( "alternative" );
        // part 1, html text
        BodyPart messageBodyPart = emailRemainder.buildHtmlTextPart ();
        multipart.addBodyPart ( messageBodyPart );

        // Add part two, the calendar
        BodyPart calendarPart = emailRemainder.buildCalendarPart ( fromMailId, "Meeting on your domain.com", "sample description" );
        multipart.addBodyPart ( calendarPart );

        // Put the multipart in message
        message.setContent ( multipart );

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

            System.out.println ("done");
    }
}



Hit Counter


View My Stats