Java 发送会议邀请到 Outlook

Java 发送会议邀请到 Outlook

系统:Win10
IDE:IntelliJ IDEA 2017.3.7
JDK:1.8.0_121
Outlook:Microsoft Office 2016

1.发件服务器配置

服务器名称:SMTP
服务器地址:smtp.126.com
SSL协议端口:465

2.发送邮件代码

需要导入mail.jar包,点击下载

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.UUID;

import javax.activation.DataHandler;
import javax.mail.Authenticator;
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;
import javax.mail.util.ByteArrayDataSource;

public class ToExchangeMail {
    protected void sendEmail() {
        try {  
            Properties props = new Properties();  
            try {
                // 使用SSL协议发送邮件
                // smtp的相关配置
                props.put("mail.smtp.host", "smtp.126.com"); // smtp地址
                props.put("mail.smtp.port", "465"); // smtp端口号
                props.put("mail.transport.protocol", "smtps"); // 邮件端口名
                props.put("mail.smtp.auth", "true"); // 是否需要身份验证
                props.put("mail.smtp.ssl.enable", "true"); // SSL加密
            } catch (Exception e) {  
                e.printStackTrace();  
            }  

            String fromEmail = "***@126.com"; // 发送邮箱
            String toEmail = "***@test.com"; // 接收邮箱
            Session session;  

            final String username = "***";//发送人的邮箱用户名(这里一定要是邮箱的用户名,不是xxx@126.com)
            final String password = "***";//发送人的邮箱密码
            class EmailAuthenticator extends Authenticator {  
                protected PasswordAuthentication getPasswordAuthentication() {  
                    return new PasswordAuthentication(username, password);  
                }  
            }
            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("Test标题"); // 会议标题
            StringBuffer buffer = new StringBuffer();
            DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // 数据库的时间格式
            DateFormat format2 = new SimpleDateFormat("yyyyMMdd:HHmmss"); // 发送到outlook的时间格式
            String beginTime = format2.format(format1.parse("2021-04-16 16:00")); // 会议开始时间
            beginTime = beginTime.replace(":", "T");
            String endTime = format2.format(format1.parse("2021-04-16 16:30")); // 会议结束时间
            endTime = endTime.replace(":", "T");
            String uuid = UUID.randomUUID().toString(); // 如果id相同的话,outlook会认为是同一个会议请求,所以使用uuid。 如果传同一ID不同内容的会议会修改上一个会议
            System.out.println("UUID:" + uuid);
            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:"  
                    + beginTime+"\n" 
                    + "DTEND:"  
                    + endTime+"\n" 
                    + "UID:"+uuid+"\n" 
                    + "CATEGORIES:"+"邮件类别"+"\n"  // 类别
                    + "DESCRIPTION:"+"会议地点"+"\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); 
            System.out.println("发送成功");
        } catch (MessagingException e) {  
            e.printStackTrace();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
}

3.测试代码

在同级包下创建测试类,运行

public class SendMailMain {
    public static void main(String[] args) {
        ToExchangeMail mail = new ToExchangeMail();
        mail.sendEmail();
    }
}

运行结果
在这里插入图片描述
进入Outlook可以看到该条会议邀请已经发送成功
在这里插入图片描述

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用C#和Outlook Interop库来发送会议邀请。以下是一个示例代码,它使用SmtpClient类和MailMessage类构造邮件发送: ``` using Microsoft.Office.Interop.Outlook; using System.Net.Mail; // 创建 Outlook 应用程序实例 var outlookApp = new Application(); // 创建一个新的 AppointmentItem 对象 var appointment = (AppointmentItem)outlookApp.CreateItem(OlItemType.olAppointmentItem); // 设置会议邀请的相关信息,例如开始时间,结束时间,主题等 appointment.Start = DateTime.Now.AddHours(1); appointment.End = DateTime.Now.AddHours(2); appointment.Subject = "会议邀请"; appointment.Location = "会议室"; appointment.Body = "这是一个测试会议邀请"; // 添加参与者 appointment.Recipients.Add("[email protected]"); // 将 AppointmentItem 对象保存到 Outlook 日历中 appointment.Save(); // 创建 MailMessage 对象并添加 Outlook 会议邀请文件作为附件 var message = new MailMessage(); message.Attachments.Add(new Attachment(appointment.GetICalExporter().SaveAsICal())); // 设置邮件相关信息,例如收件人,主题等 message.To.Add("[email protected]"); message.Subject = appointment.Subject; // 创建 SmtpClient 对象并发送邮件 var smtpClient = new SmtpClient("smtp.example.com"); smtpClient.Send(message); ``` 请注意,本示例代码使用 Outlook 应用程序和 SmtpClient 类来发送邮件,因此您需要在本地安装 Outlook 应用程序,或者您也可以使用其他支持 iCalendar 文件格式的邮件客户端来接收会议邀请

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李晋江

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值