使用日历取消会议请求
Contents
[
Hide
]
您可以使用 Aspose.Email 的 Appointment 类对象发送会议取消请求。需要拥有原始会议请求信息才能取消请求。本文示例首先发送会议请求,将信息保存到数据库,然后基于消息 ID 取消请求。
发送会议请求
在我们能够之前 取消会议请求,我们必须发送一些。
- 首先创建 SmtpClient 类型的实例用于发送消息。
- 将所有参会者信息保存到 MailAddressCollection 集合中。
- 创建 MailMessage 类的实例并设置必要属性,如 From、To 和 Subject。
- 创建 Appointment 类型的实例,并提供地点、开始时间、结束时间、组织者和参会者信息。
- 将所有信息保存到数据库中。与数据库相关的工作在 SaveIntoDB 方法中完成。
以下代码片段演示了如何发送会议请求。
class Attendees {
public String MessageId;
public String EmailAddress;
public String DisplayName;
}
class Message {
public String MessageId;
public String From;
public String Subject;
public String Body;
public String AppLocation;
public Date AppStartDate;
public Date AppEndDate;
public String AppSummary;
public String AppDescription;
}
public void send(Attendees[] attendeesArr, String from, String appLocation, Date appStartDate, Date appEndDate) {
try {
// Create an instance of SMTPClient
SmtpClient client = new SmtpClient("MailServer", "Username", "Password");
// Get the attendees
MailAddressCollection attendees = new MailAddressCollection();
for (Attendees a : attendeesArr) {
attendees.addItem(new MailAddress(a.EmailAddress, a.DisplayName));
}
// Create an instance of MailMessage for sending the invitation
MailMessage msg = new MailMessage();
// Set from address, attendees
msg.setFrom(new MailAddress(from));
msg.setTo(attendees);
// Create am instance of Appointment
Appointment app = new Appointment(appLocation, appStartDate, appEndDate, new MailAddress(from), attendees);
app.setSummary("Monthly Meeting");
app.setDescription("Please confirm your availability.");
msg.addAlternateView(app.requestApointment());
// Save the info to the database
if (saveIntoDB(msg, app) == true) {
// Save the message and Send the message with the meeting request
msg.save(msg.getMessageId() + ".eml", SaveOptions.getDefaultEml());
client.send(msg);
System.out.println("message sent");
}
} catch (Exception ex) {
System.err.println(ex);
}
}
private boolean saveIntoDB(MailMessage msg, Appointment app) {
// Save Message and Appointment information
Message messageRow = new Message();
messageRow.MessageId = msg.getMessageId();
messageRow.From = msg.getFrom().getAddress();
messageRow.Subject = msg.getSubject();
messageRow.Body = msg.getBody();
messageRow.AppLocation = app.getLocation();
messageRow.AppStartDate = app.getStartDate();
messageRow.AppEndDate = app.getEndDate();
messageRow.AppSummary = app.getSummary();
messageRow.AppDescription = app.getDescription();
addToDB(messageRow);
// Save attendee information
for (MailAddress address : app.getAttendees()) {
Attendees attendeesRow = new Attendees();
attendeesRow.MessageId = msg.getMessageId();
attendeesRow.EmailAddress = address.getAddress();
attendeesRow.DisplayName = address.getDisplayName();
addToDB(attendeesRow);
}
return true;
}
取消会议请求
要取消会议请求,首先获取邮件的消息 ID。由于我们在本示例中已将此信息保存在数据库中,因此可以轻松重新获取。
- 选择要发送取消请求的邮件。
- 点击 Send Cancel Request 发送请求。
- 查询数据库以获取与参会者、消息和日历相关的信息。
- 使用从数据库检索的信息创建 Calendar 类和 MailMessage 类的实例。
- 使用 Appointment.cancelAppointment() 方法发送取消请求。
- 使用 SMTP 发送邮件。
以下代码片段演示了如何取消会议请求。
public void cancel(String messageId) {
// Get the message and calendar information from the database get the attendee information
// Get the attendee information in reader
Attendees[] attendeesRows = getAttendeesFromDB(messageId);
// Create a MailAddressCollection from the attendees found in the database
MailAddressCollection attendees = new MailAddressCollection();
for (Attendees attendeesRow : attendeesRows) {
attendees.addItem(new MailAddress(attendeesRow.EmailAddress, attendeesRow.DisplayName));
}
// Get message and calendar information
Message messageRow = getMessageFromDB(messageId);
// Create the Calendar object from the database information
Appointment app = new Appointment(messageRow.AppLocation, messageRow.AppSummary, messageRow.AppDescription, messageRow.AppStartDate, messageRow.AppEndDate,
new MailAddress(messageRow.From), attendees);
// Create message and Set from and to addresses and Add the cancel meeting request
MailMessage msg = new MailMessage();
msg.setFrom(new MailAddress(messageRow.From));
msg.setTo(attendees);
msg.setSubject("Cencel meeting");
msg.addAlternateView(app.cancelAppointment());
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587, "user@gmail.com", "password");
smtp.send(msg);
System.out.println("cancellation request successfull");
}