使用javamail发送会议邀请

网上找了很多关于javamail发送会议请求的文章,终于成功了,没有使用ical4j,发送的outlook直接打开就是一个会议请求,而不是以附件的形式存在。具体的注意点在代码中以注释的形式标明。

DESCRIPTION中如果想换行,换行符为:=0D=0A

关于icalendar的规范,请看:https://rsync.tools.ietf.org/html/rfc5545

public class Email {
	private static String EMAIL_CONFIG = "email_config.properties";
	private Properties emailProp = new Properties();
	public Email() {
		InputStream is = getClass().getResourceAsStream("/"+EMAIL_CONFIG);
		try {
			emailProp.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private class EmailAuthenticator extends Authenticator {
		protected PasswordAuthentication getPasswordAuthentication() {
			String userId = emailProp.getProperty("userId", "");
			String password = emailProp.getProperty("password", "");
			return new PasswordAuthentication(userId, password);
		}
	}

	public void send() throws Exception {

		try {
			String fromEmail = emailProp.getProperty("fromEmail", "");
			String toEmail=emailProp.getProperty("toEmail", ""); 
			Properties props = new Properties();
			try {
				props.put("mail.smtp.port", "587");
				props.put("mail.smtp.host", "smtp.gmail.com");
				props.put("mail.transport.protocol", "smtp");
				props.put("mail.smtp.auth", "true");
				props.put("mail.smtp.starttls.enable", "true");
				props.put("mail.smtp.ssl", "true");

			} catch (Exception e) {
				e.printStackTrace();
			}

			Session session;
			Authenticator authenticator = new EmailAuthenticator();
			session = Session.getInstance(props, authenticator);
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(fromEmail));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
			message.setSubject("Outlook Meeting Request Using JavaMail");
			StringBuffer buffer = new StringBuffer();
			buffer.append("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:"+toEmail+"\n"
					+ "ORGANIZER:MAILTO:"+toEmail+"\n"
					+ "DTSTART:20120302T060000Z\n"
					+ "DTEND:20120302T070000Z\n"
					+ "LOCATION:Conference room\n"
					+ "UID:"+UUID.randomUUID().toString()+"\n"//如果id相同的话,outlook会认为是同一个会议请求,所以使用uuid。
					+ "CATEGORIES:SuccessCentral Reminder\n"
					+ "DESCRIPTION:This the description of the meeting.<br>asd;flkjasdpfi\n\n"
					+ "SUMMARY:Test meeting request\n" + "PRIORITY:5\n"
					+ "CLASS:PUBLIC\n" + "BEGIN:VALARM\n"
					+ "TRIGGER:-PT15M\n" + "ACTION:DISPLAY\n"
					+ "DESCRIPTION:Reminder\n" + "END:VALARM\n"
					+ "END:VEVENT\n" + "END:VCALENDAR");
			BodyPart messageBodyPart = new MimeBodyPart();
			// 测试下来如果不这么转换的话,会以纯文本的形式发送过去,
			//如果没有method=REQUEST;charset=\"UTF-8\",outlook会议附件的形式存在,而不是直接打开就是一个会议请求
			messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(buffer.toString(), 
					"text/calendar;method=REQUEST;charset=\"UTF-8\"")));
			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);
			message.setContent(multipart);
			Transport.send(message);
		} catch (MessagingException me) {
			me.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		try {
			Email email = new Email();
			email.send();
			System.out.println("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值