Hủy Yêu cầu Họp bằng Lịch

Bạn có thể gửi yêu cầu hủy họp bằng Aspose.Email sử dụng đối tượng lớp Appointment. Bạn cần có thông tin yêu cầu họp gốc để hủy. Ví dụ trong bài này đầu tiên gửi yêu cầu họp, lưu thông tin vào cơ sở dữ liệu và sau đó hủy yêu cầu dựa trên ID tin nhắn.

Gửi Yêu cầu Họp

Trước khi chúng ta có thể hủy yêu cầu họp, chúng ta cần gửi một vài cái:

  1. Đầu tiên tạo một đối tượng kiểu SmtpClient để gửi tin nhắn.
  2. Lưu toàn bộ thông tin người tham dự vào bộ sưu tập MailAddressCollection.
  3. Tạo một đối tượng của lớp MailMessage và các thuộc tính cần thiết như From, To và Subject.
  4. Tạo một đối tượng kiểu Appointment và cung cấp thông tin địa điểm, thời gian bắt đầu, thời gian kết thúc, người tổ chức và người tham dự.
  5. Lưu tất cả thông tin vào cơ sở dữ liệu. Các công việc liên quan đến DB được thực hiện trong phương thức SaveIntoDB.

Đoạn mã sau cho bạn cách gửi yêu cầu họp.

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;
}

Hủy Yêu cầu Họp

Để hủy yêu cầu họp, trước tiên lấy ID tin nhắn email. Vì chúng tôi đã lưu thông tin này trong cơ sở dữ liệu cho ví dụ này, nên có thể dễ dàng lấy lại.

  1. Chọn tin nhắn để gửi yêu cầu hủy.
  2. Nhấn Send Cancel Request để gửi yêu cầu.
  3. Truy vấn cơ sở dữ liệu để lấy thông tin người tham dự, tin nhắn và lịch.
  4. Tạo các đối tượng của lớp Calendar và lớp MailMessage bằng thông tin lấy từ cơ sở dữ liệu.
  5. Sử dụng phương thức Appointment.cancelAppointment() để gửi yêu cầu hủy.
  6. Gửi email bằng SMTP.

Đoạn mã sau cho bạn cách hủy yêu cầu họp.

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");
}