Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Silambarasann/b07b4238a0f4c9e26d3e to your computer and use it in GitHub Desktop.
Save Silambarasann/b07b4238a0f4c9e26d3e to your computer and use it in GitHub Desktop.
Sending Email request with calendar integration using Java
/**
* method to send mail request along with calendar intergration for interview
* @author simbu
* @param emails
* @param string
* @param subject
* @throws Exception
*/
public static 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");
// **************//
// Provide SMTP Details
final String username = "no-reply@sss.com"; // your username
final String password = "no-reply@ddd.com003"; // your password
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.startTLS.enable", "true");
props.put("mail.smtp.host", "smtp.ss.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", "smtp.ssss.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);
} catch (MessagingException e) {
e.printStackTrace();
log.error(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 static 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 static 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
// 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 candidate\n" + "UID:324\n" + "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE: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");
return calendarPart;
}
@Silambarasann
Copy link
Author

This java code is used to sending email that contains accept, decline and later like meeting request , and can integrate with any email's calendar.

@ahmednrana
Copy link

What is iCalendarDateFormat on line 105

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment