Send a Meeting Request

You can send a meeting cancel request with Aspose.Network using Calendar object. You need to have the original meeting request information in order to cancel the request. At first, we will send a meeting request, save the information in a database and then cancel the request on the basis of the MessageID.

The code snippets are taken from a demo. You can get the full demo and related SQL Server database file from Aspose.Network for .NET Demos. We first create an instance of type SmtpClient for sending the message. For attendees 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 foreach loop on Rows collection of the grid, we save all the attendees information in MailAddressCollection. After that, we create an instance of MailMessage class and necessary properties like From, To and Subject. Then, we create instance of type Calendar and give location, start time, end time, organizers and attendees information. After creating these objects, we save all the information in SQL Server database. DB related work is being done in SaveIntoDB method.

Following is the code for sending the meeting request.

[C#]

try

{

// create 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 instance of MailMessage for sending the invitation
MailMessage msg = new MailMessage();
// set from address
msg.From = txtFrom.Text;
// set the attendees
msg.To = attendees;

// create instance of Calendar
Calendar cal = new Calendar(txtLocation.Text, dtTimeFrom.Value, dtTimeTo.Value, txtFrom.Text, attendees);
cal.Summary = "Monthly Meeting";
cal.Description = "Please confirm your availability.";
msg.AddCalendar(cal);

// save the info in DB
if (SaveIntoDB(msg, cal) == true)
{
// save the message and icalendar.
cal.Save(msg.MessageId + ".ics");
msg.Save(msg.MessageId + ".eml", MessageFormat.Eml);
// send the message with the meeting request
client.Send(msg);
MessageBox.Show("message sent");
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

private bool SaveIntoDB(MailMessage msg, Calendar cal)
{
try
{

// save message and calendar 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.TextBody + "' , '" + cal.UniqueId + "' , '" + cal.SequenceId + @"' ,
'" + cal.Location + "' , '" + cal.StartDate + "' , '" + cal.EndDate + "' , '" + cal.Description + @"' ,
'" + cal.Summary + "' , 1); ";
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSQLInsertIntoMessage, null);

// save attendents 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() + "' );";
}
}
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSQLInsertIntoAttendees, null);
// if records are successfully inserted into DB, return true
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
// return false in case of exception
return false;
}

}

[VB.NET]

Try
' create instance of SMTPClient
Dim client As SmtpClient = New SmtpClient(txtMailServer.Text, txtUsername.Text, txtPassword.Text)
' get the attendees
Dim attendees As MailAddressCollection = New MailAddressCollection()
For Each row As DataGridViewRow In dgAttendees.Rows
If Not row.Cells("EmailAddress").Value Is Nothing Then
' 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()))
End If
Next row
' create instance of MailMessage for sending the invitation
Dim msg As MailMessage = New MailMessage()
' set from address
msg.From = txtFrom.Text
' set the attendees
msg.To = attendees

' create instance of Calendar
Dim cal As Calendar = New Calendar(txtLocation.Text, dtTimeFrom.Value, dtTimeTo.Value, txtFrom.Text, attendees)
cal.Summary = "Monthly Meeting"
cal.Description = "Please confirm your availability."
msg.AddCalendar(cal)

' save the info in DB
If SaveIntoDB(msg, cal) = True Then
' save the message and icalendar.
cal.Save(msg.MessageId & ".ics")
msg.Save(msg.MessageId & ".eml", MessageFormat.Eml)
' send the message with the meeting request
client.Send(msg)
MessageBox.Show("message sent")
End If

Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

Private Function SaveIntoDB(ByVal msg As MailMessage, ByVal cal As Calendar) As Boolean
Try
' save message and calendar information
Dim strSQLInsertIntoMessage As String = "INSERT INTO Message (MessageID, Subject, FromEmailAddress, FromDisplayName, Body," & ControlChars.CrLf & " CalUniqueID, CalSequenceID, CalLocation, CalStartTime, CalEndTime, Caldescription, CalSummary, MeetingStatus)" & ControlChars.CrLf & " VALUES ('" & msg.MessageId & "' , '" & msg.Subject & "' , '" & msg.From(0).Address & "' , '" & msg.From(0).DisplayName & "' , '" & msg.TextBody & "' , '" & cal.UniqueId & "' , '" & cal.SequenceId & "' ," & ControlChars.CrLf & " '" & cal.Location & "' , '" & cal.StartDate & "' , '" & cal.EndDate & "' , '" & cal.Description & "' , " & ControlChars.CrLf & " '" & cal.Summary & "' , 1); "
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSQLInsertIntoMessage, Nothing)

' save attendents information
Dim strSQLInsertIntoAttendees As String = ""
For Each row As DataGridViewRow In dgAttendees.Rows
If Not row.Cells("EmailAddress").Value Is Nothing Then
strSQLInsertIntoAttendees &= "INSERT INTO Attendent (MessageID, EmailAddress, DisplayName) VALUES (" & "'" & msg.MessageId & "' , '" & row.Cells("EmailAddress").Value & "' , '" & row.Cells("FirstName").Value.ToString() & " " & row.Cells("LastName").Value.ToString() & "' );"
End If
Next row
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSQLInsertIntoAttendees, Nothing)
' if records are successfully inserted into DB, return true
Return True
Catch ex As Exception
MessageBox.Show(ex.Message)
' return false in case of exception
Return False
End Try
End Function

Canceling Meeting Request

For creating a cancel meeting request, we first need to get the MessageID of the email message. Since, we have saved this information in a database; we can easily get this in our example. We have used a grid to load all the sent messages.

After selecting the row for which we want to send the cancel meeting request, we click on the “Send Cancel Request” button to send the request. In the code, we get the ID from the selected row of the grid and query the database for getting the attendees, message and calendar related information. Create the instances of the Calendar and MailMessage classes using the information that is retrieved from the database. We use Calendar.CreateCancelRequestFrom() method which returns instance of type AlternateView and and add it to the AlternateViews collection of the MailMessage. Then, we just send the mail using SmtpClient. This will send the cancel request to the attendees.

Following is the code for canceling the meeting request.

[C#]

try
{
// get the messageid of the selected row
string strMessageID = dgMeetings.SelectedRows[0].Cells["MessageID"].Value.ToString();

// get message and calendar information from the DB
// get the attendees information
string strSQLGetAttendees = "SELECT * FROM Attendent WHERE MessageID = '" + strMessageID + "' ";

// get the attendees information in reader
SqlDataReader rdAttendees = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text,
strSQLGetAttendees, null);
// create MailAddressCollection from the attendees found in the DB
MailAddressCollection attendees = new MailAddressCollection();
while (rdAttendees.Read())
{
attendees.Add(new MailAddress(rdAttendees["EmailAddress"].ToString(),
rdAttendees["DisplayName"].ToString()));
}

// get message and calendar information
string strSQLGetMessage = "SELECT * FROM [Message] WHERE MessageID = '" + strMessageID + "' ";
// get the reader for the above query
SqlDataReader rdMessage = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSQLGetMessage, null);
rdMessage.Read();
// create the Calendar object from the DB information
Calendar cal = new Calendar(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 alternate view from the calendar object for meeting cancellation required
AlternateView v = Calendar.CreateCancelRequestFrom(cal, rdMessage["CalUniqueID"].ToString(), int.Parse(rdMessage["CalSequenceID"].ToString()));
// create message
MailMessage msg = new MailMessage();
// set from and to addresses
msg.From = new MailAddress(rdMessage["FromEmailAddress"].ToString(), "");
msg.To = attendees;
msg.Subject = "Cencel meeting";
// add the cancel meeting request
msg.AlternateViews.Add(v);
// send the message
SmtpClient smtp = new SmtpClient("mail.domain.com", "user", "password");
smtp.Send(msg);
MessageBox.Show("cancellation request successfull");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

[VB.NET]

Try
' get the messageid of the selected row
Dim strMessageID As String = dgMeetings.SelectedRows(0).Cells("MessageID").Value.ToString()

' get message and calendar information from the DB
' get the attendees information
Dim strSQLGetAttendees As String = "SELECT * FROM Attendent WHERE MessageID = '" & strMessageID & "' "
' get the attendees information in reader
Dim rdAttendees As SqlDataReader = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSQLGetAttendees, Nothing)
' create MailAddressCollection from the attendees found in the DB
Dim attendees As MailAddressCollection = New MailAddressCollection()
Do While rdAttendees.Read()
attendees.Add(New MailAddress(rdAttendees("EmailAddress").ToString(), rdAttendees("DisplayName").ToString()))
Loop

' get message and calendar information
Dim strSQLGetMessage As String = "SELECT * FROM [Message] WHERE MessageID = '" & strMessageID & "' "
' get the reader for the above query
Dim rdMessage As SqlDataReader = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSQLGetMessage, Nothing)
rdMessage.Read()
' create the Calendar object from the DB information
Dim cal As Calendar = New Calendar(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 alternate view from the calendar object for meeting cancellation required
Dim v As AlternateView = Calendar.CreateCancelRequestFrom(cal, rdMessage("CalUniqueID").ToString(), Integer.Parse(rdMessage("CalSequenceID").ToString()))
' create message
Dim msg As MailMessage = New MailMessage()
' set from and to addresses
msg.From = New MailAddress(rdMessage("FromEmailAddress").ToString(), "")
msg.To = attendees
msg.Subject = "Cencel meeting"
' add the cancel meeting request
msg.AlternateViews.Add(v)
' send the message
Dim smtp As SmtpClient = New SmtpClient("mail.domain.com", "user", "password")
smtp.Send(msg)
MessageBox.Show("cancellation request successfull")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

More about Aspose.Network for .NET

Aspose.Network is a suite of .NET components for network programming with support for .NET logging framework and Microsoft Exchange Server. Now you can read and write Outlook Message files using Aspose.Network. It provides iCalendar engine and SSL support for SMTP, POP3 & IMAP protocols with other mail merge features. You can import & export emails in MHT & EML formats. It supports all the features of SMTP, MIME, S/MIME, POP3, FTP, WhoIs, DNS, ICMP, IMAP, HTTP, SOCKS 4/4A & SOCKS 5 components.

- Homepage of Aspose.Network for .NET.
- Download Evaluation Version of Aspose.Network for .NET.
- Online Documentation of Aspose.Network for .NET.
- Demos of Aspose.Network for .NET.
- Post your technical questions/queries to Aspose.Network for .NET Forum.
- Receive notifications about latest news and supported features by subscribing to Aspose.Network for .NET blog.

Contact Information
Suite 119, 272 Victoria Avenue
Chatswood, NSW, 2067
Australia
Aspose - The .NET and Java component publisher
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.94651