Я пытался использовать ical весной, сейчас у меня есть два файла: один для создания события, другой для отправки по электронной почте после создания события.
вот мой код:
public class cal {
public static String mailfrom = "example@mail.com";
public static String mailto = "example@mail.com";
public static Multipart getContent() throws Exception {
// create a timezone
TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
TimeZone timezone = registry.getTimeZone("United Kingdom/Bristol");
VTimeZone vTimeZone = timezone.getVTimeZone();
//event start
java.util.Calendar startDate = new GregorianCalendar();
startDate.setTimeZone(timezone);
startDate.set(java.util.Calendar.MONTH, java.util.Calendar.MARCH);
startDate.set(java.util.Calendar.DAY_OF_MONTH, 15);
startDate.set(java.util.Calendar.YEAR, 2020);
startDate.set(java.util.Calendar.HOUR_OF_DAY, 12);
startDate.set(java.util.Calendar.MINUTE, 0);
startDate.set(java.util.Calendar.SECOND, 0);
//event end
java.util.Calendar endDate = new GregorianCalendar();
endDate.setTimeZone(timezone);
endDate.set(java.util.Calendar.MONTH, java.util.Calendar.MARCH);
endDate.set(java.util.Calendar.DAY_OF_MONTH, 15);
endDate.set(java.util.Calendar.YEAR, 2020);
endDate.set(java.util.Calendar.HOUR_OF_DAY, 13);
endDate.set(java.util.Calendar.MINUTE, 0);
endDate.set(java.util.Calendar.SECOND, 0);
//instance
String eventName = "testifwork";
DateTime start = new DateTime(startDate.getTime());
DateTime end = new DateTime(endDate.getTime());
VEvent meeting = new VEvent(start, end, eventName);
//add timezone
meeting.getProperties().add(vTimeZone.getTimeZoneId());
//generate unique uid
UidGenerator uidGenerator = new RandomUidGenerator();
Uid uid = uidGenerator.generateUid();
meeting.getProperties().add(uid);
//add attendee
Attendee attendee1 = new Attendee(URI.create("mailto:example@mail.com"));
attendee1.getParameters().add(Role.REQ_PARTICIPANT);
attendee1.getParameters().add(new Cn("develop 1"));
meeting.getProperties().add(attendee1);
Attendee attendee2 = new Attendee(URI.create("mailto:example@mail.com"));
attendee1.getParameters().add(Role.REQ_PARTICIPANT);
attendee1.getParameters().add(new Cn("develop 2"));
meeting.getProperties().add(attendee2);
Attendee attendee3 = new Attendee(URI.create("mailto:example@mail.com"));
attendee1.getParameters().add(Role.REQ_PARTICIPANT);
attendee1.getParameters().add(new Cn("develop 3"));
meeting.getProperties().add(attendee3);
//create calendar
net.fortuna.ical4j.model.Calendar icsCalendar = new Calendar();
icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
icsCalendar.getProperties().add(CalScale.GREGORIAN);
icsCalendar.getProperties().add(Version.VERSION_2_0);
icsCalendar.getProperties().add(Method.REQUEST);
// add the event
icsCalendar.getComponents().add(meeting);
//make it binary
CalendarOutputter calendarOutputter = new CalendarOutputter(false);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
calendarOutputter.output(icsCalendar, outputStream);
byte[] mailbyte = outputStream.toByteArray();
BodyPart mimeBodyPart = new MimeBodyPart(); //Multipurpose Internet Mail Extensions
mimeBodyPart.setContent(mailbyte, "text/calendar;method=REQUEST;charset=UTF-8");
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.setSubType("related"); // relate is Embedded Resource
mimeMultipart.addBodyPart(mimeBodyPart);
return mimeMultipart;
}
public static void main(String[] args) {
Properties properties = new Properties();
//mail protocol
properties.put("mail.transport.protocol", "smtp");
//url
properties.put("mail.smtp.host", "smtp-mail.outlook.com");
properties.put("mail.smtp.auth", "true");
//account
Authenticator authentication = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String user = properties.getProperty("user","example@mail.com");
String password = properties.getProperty("password","password");
return new PasswordAuthentication(user,password);
}
};
Session mailSession = Session.getInstance(properties, authentication);
Message message = new MimeMessage(mailSession);
try {
//mail from
message.setFrom(new InternetAddress(mailfrom));
//mail to
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailto));
//mail time
message.setSentDate(new java.util.Date());
//mail heading
String subject = "test";
message.setSubject(subject);
Multipart multipart = getContent();
message.setContent(multipart);
message.saveChanges();
} catch (Exception exception) {
}
System.out.println(sendmail(message));
}
public static Boolean sendmail(Message message) {
//sent mail
try {
Transport.send(message);
return true;
} catch (Exception e) {
//mail invalid
System.out.println(e);
return false;
}
}
второй:
public class email {
public static void main(String[] args){
sendEmail();
}
private static Properties properties;
private static Session session;
public static void sendEmail(){
try {
String name = "name";
properties = new Properties();
String mailfrom = properties.getProperty("mail@example.com");
String mailto = properties.getProperty("mail@example.com");
properties.put("mail.imap.port", "25");
properties.put("mail.imap.host", "smtp-mail.outlook.com");
properties.put("mail.smtp.ssl.trust", "smtp-mail.outlook.com");
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl", "true");
properties.put("mail.debug", "true");
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String user = properties.getProperty("user","mail@example.com");
String password = properties.getProperty("password","password");
return new PasswordAuthentication(user,password);
}
};
session = Session.getInstance(properties, authenticator);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(mailfrom));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(mailto));
mimeMessage.setSubject("is a test");
String startTime = getUtc("2020-04-10 14:00");
String endTime = getUtc("2020-04-10 16:00");
BodyPart messageBodyPart = new MimeBodyPart();
String buffer = "BEGIN:VCALENDAR\n"
+ "PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n"
+ "VERSION:2.0\n"
+ "METHOD:REQUEST\n"
+ "BEGIN:VEVENT\n"
+ "ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:"+"mail@example.com"+"\n"
+ "ORGANIZER:MAILTO:"+"mail@example.com"+"\n"
+ "DTSTART:" + startTime + "\n"
+ "DTEND:" + endTime + "\n"
+ "LOCATION: emergency department\n"
+ "UID:" + UUID.randomUUID().toString() + "\n"
+ "CATEGORIES:\n"
+ "CLASS:PUBLIC\n" + "BEGIN:VALARM\n"
+ "TRIGGER:-PT15M\n" + "ACTION:DISPLAY\n"
+ "DESCRIPTION:Reminder\n" + "END:VALARM\n"
+ "END:VEVENT\n" + "END:VCALENDAR";
messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(buffer,"text/calendar;method=REQUEST;charset=\"UTF - 8\"")));
MimeMultipart mimeMultipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setText("name:" + name);
mimeMultipart.addBodyPart(mimeBodyPart);
mimeMultipart.addBodyPart(messageBodyPart);
mimeMessage.setContent(mimeMultipart);
mimeMessage.setSentDate(new Date());
mimeMessage.saveChanges();
Transport.send(mimeMessage);
}catch (Exception e){
e.printStackTrace();
}
}
private static String getUtc(String string){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm");
long second = 0;
try {
second = simpleDateFormat.parse(string).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
long currentTime = second + 60 * 60 * 1000;
Date date = new Date(currentTime);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String nowTime;
nowTime = dateFormat.format(date);
return nowTime.replace("-","").replace(" ", "T").replace(":","");
}
}
Теперь я не уверен, как на самом деле использовать их весной, если я буду использовать их в контроллере или в приложении. Как именно я должен это реализовать? Нужно ли иметь файл ics вручную или я должен сделать это в первом файле