لغو درخواست‌های جلسه با تقویم

می‌توانید با استفاده از شیء کلاس Appointment در Aspose.Email یک درخواست لغو جلسه ارسال کنید. برای لغو درخواست به اطلاعات اصلی درخواست جلسه نیاز دارید. مثال در این مقاله ابتدا یک درخواست جلسه ارسال می‌کند، اطلاعات را در پایگاه داده ذخیره می‌کند و سپس درخواست را بر پایه شناسه پیام لغو می‌کند.

ارسال درخواست‌های جلسه

قبل از اینکه بتوانیم لغو درخواست‌های جلسه، ما باید برخی را ارسال کنیم:

  1. ابتدا یک نمونه از نوع SmtpClient برای ارسال پیام ایجاد کنید.
  2. تمام اطلاعات شرکت‌کنندگان را در مجموعه MailAddressCollection ذخیره کنید.
  3. یک نمونه از کلاس MailMessage و ویژگی‌های ضروری مانند From، To و Subject ایجاد کنید.
  4. یک نمونه از نوع Appointment ایجاد کنید و اطلاعات مکان، زمان شروع، زمان پایان، سازمان‌دهندگان و شرکت‌کنندگان را تعیین کنید.
  5. تمام اطلاعات را در یک پایگاه داده ذخیره کنید. کارهای مربوط به پایگاه داده در متد 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;
}

لغو درخواست جلسه

برای لغو درخواست جلسه، ابتدا شناسه پیام ایمیل را به دست آورید. از آنجا که این اطلاعات برای این مثال در پایگاه داده ذخیره شده است، می‌توانیم به راحتی دوباره آن را دریافت کنیم.

  1. انتخاب پیام برای ارسال درخواست لغو.
  2. Send Cancel Request را کلیک کنید تا درخواست ارسال شود.
  3. پایگاه داده را برای دریافت اطلاعات شرکت‌کنندگان، پیام و تقویم پرس و جو می‌کند.
  4. یک نمونه از کلاس Calendar و کلاس MailMessage با استفاده از اطلاعات بازیابی‌شده از پایگاه داده ایجاد کنید.
  5. از متد Appointment.cancelAppointment() برای ارسال درخواست لغو استفاده کنید.
  6. ایمیل را با استفاده از 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");
}