Cancelling Meeting Requests with Calendar
You can send a meeting cancellation request with Aspose.Email using the Appointment Class object. You need to have the original meeting request information to cancel the request. The example in this article first sends a meeting request, saves the information in a database and then cancels the request based of the message ID.
Sending Meeting Requests
Before we can cancel meeting requests, we have to send some out:
- First create an instance of type SmtpClient for sending the message.
- To gather attendee information, we have created a data grid so that users can enter the names and addresses of people whom the invitation should be sent.
- After doing a for-each loop on the Rows collection of the grid, save all the attendee information in the MailAddressCollection collection.
- Create an instance of the MailMessage class and necessary properties like From, To and Subject.
- Create an instance of type Appointment and give location, start time, end time, organizers and attendees information.
- Save all the information in an SQL Server database. DB related work is being done in the SaveIntoDB method.
The following code snippet shows you how to send meeting requests.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
try | |
{ | |
// Create an instance of SMTPClient | |
SmtpClient client = new SmtpClient(txtMailServer.Text, txtUsername.Text, txtPassword.Text); | |
// Get the attendees | |
MailAddressCollection attendees = new MailAddressCollection(); | |
foreach (DataGridViewRow row in dgAttendees.Rows) | |
{ | |
if (row.Cells["EmailAddress"].Value != null) | |
{ | |
// Get names and addresses from the grid and add to MailAddressCollection | |
attendees.Add(new MailAddress(row.Cells["EmailAddress"].Value.ToString(), | |
row.Cells["FirstName"].Value.ToString() + " " + row.Cells["LastName"].Value.ToString())); | |
} | |
} | |
// Create an instance of MailMessage for sending the invitation | |
MailMessage msg = new MailMessage(); | |
// Set from address, attendees | |
msg.From = txtFrom.Text; | |
msg.To = attendees; | |
// Create am instance of Appointment | |
Appointment app = new Appointment(txtLocation.Text, dtTimeFrom.Value, dtTimeTo.Value, txtFrom.Text, attendees); | |
app.Summary = "Monthly Meeting"; | |
app.Description = "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.MessageId + ".eml", SaveOptions.DefaultEml); | |
client.Send(msg); | |
MessageBox.Show("message sent"); | |
} | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
private bool SaveIntoDB(MailMessage msg, Appointment app) | |
{ | |
try | |
{ | |
// Save message and Appointment information | |
string strSQLInsertIntoMessage = @"INSERT INTO Message (MessageID, Subject, FromEmailAddress, FromDisplayName, Body, | |
CalUniqueID, CalSequenceID, CalLocation, CalStartTime, CalEndTime, Caldescription, CalSummary, MeetingStatus) | |
VALUES ('" + msg.MessageId + "' , '" + msg.Subject + "' , '" + msg.From[0].Address + "' , '" + | |
msg.From[0].DisplayName + "' , '" + msg.Body + "' , '" + app.UniqueId + "' , '" + app.SequenceId + @"' , | |
'" + app.Location + "' , '" + app.StartDate + "' , '" + app.EndDate + "' , '" + app.Description + @"' , | |
'" + app.Summary + "' , 1); "; | |
ExecuteNonQuery(strSQLInsertIntoMessage); | |
// Save attendee information | |
string strSQLInsertIntoAttendees = ""; | |
foreach (DataGridViewRow row in dgAttendees.Rows) | |
{ | |
if (row.Cells["EmailAddress"].Value != null) | |
{ | |
strSQLInsertIntoAttendees += "INSERT INTO Attendent (MessageID, EmailAddress, DisplayName) VALUES (" + "'" + msg.MessageId + "' , '" + row.Cells["EmailAddress"].Value + "' , '" + row.Cells["FirstName"].Value.ToString() + " " + row.Cells["LastName"].Value.ToString() + "' );"; | |
ExecuteNonQuery(strSQLInsertIntoAttendees); | |
} | |
} | |
// If records are successfully inserted into the database, return true | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); // Return false in case of exception | |
return false; | |
} | |
} |
Cancelling Meeting Request
To cancel a meeting request, first get the email message’s message ID. Since we have saved this information in a database for this example, we can easily get it again. We have used a grid to load all the sent messages. The screenshot of the form is as below:
- Selecting the row for which to send the cancellation request.
- Click Send Cancel Request to send the request.
- The code gets the ID from the selected row of the grid and queries the database to get the attendee, message and calendar related information.
- Create an instances of the Calendar Class and MailMessage class classes using the information retrieved from the database.
- Use the Appointment.CancelAppointment() method to send the cancellation request.
- Send the mail using the SMTP.
The following code snippet shows you how to cancel the meeting request.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
try | |
{ | |
OpenConnection(); | |
// Get the message ID of the selected row | |
string strMessageID = dgMeetings.SelectedRows[0].Cells["MessageID"].Value.ToString(); | |
// Get the message and calendar information from the database get the attendee information | |
string strSQLGetAttendees = "SELECT * FROM Attendent WHERE MessageID = '" + strMessageID + "' "; | |
// Get the attendee information in reader | |
SqlDataReader rdAttendees = ExecuteReader(strSQLGetAttendees); | |
// Create a MailAddressCollection from the attendees found in the database | |
MailAddressCollection attendees = new MailAddressCollection(); | |
while (rdAttendees.Read()) | |
{ | |
attendees.Add(new MailAddress(rdAttendees["EmailAddress"].ToString(), rdAttendees["DisplayName"].ToString())); | |
} | |
rdAttendees.Close(); | |
// Get message and calendar information | |
string strSQLGetMessage = "SELECT * FROM [Message] WHERE MessageID = '" + strMessageID + "' "; | |
// Get the reader for the above query | |
SqlDataReader rdMessage = ExecuteReader(strSQLGetMessage); | |
rdMessage.Read(); | |
// Create the Calendar object from the database information | |
Appointment app = new Appointment(rdMessage["CalLocation"].ToString(), rdMessage["CalSummary"].ToString(), rdMessage["CalDescription"].ToString(), | |
DateTime.Parse(rdMessage["CalStartTime"].ToString()), DateTime.Parse(rdMessage["CalEndTime"].ToString()), new MailAddress(rdMessage["FromEmailAddress"].ToString(), rdMessage["FromDisplayName"].ToString()), attendees); | |
// Create message and Set from and to addresses and Add the cancel meeting request | |
MailMessage msg = new MailMessage(); | |
msg.From = new MailAddress(rdMessage["FromEmailAddress"].ToString(), ""); | |
msg.To = attendees; | |
msg.Subject = "Cencel meeting"; | |
msg.AddAlternateView(app.CancelAppointment()); | |
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587, "user@gmail.com", "password"); | |
smtp.Send(msg); | |
MessageBox.Show("cancellation request successfull"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
finally | |
{ | |
CloseConnection(); | |
} |