Imports System.Net.Mail

Module Module1

    Sub Main()
        Dim MyAppt As New AppointmentBLL(RoundUpToQuarterHour(Now.AddMinutes(60)), RoundUpToQuarterHour(Now.AddMinutes(90)), "Subject", "Open to discuss", "Location", "To@to.org", "To@to.org", "From@From.org", "From@From.org")
        MyAppt.EmailAppointment()
    End Sub

    Function RoundUpToQuarterHour(ByVal theDate As Date) As Date
        Do Until theDate.Minute Mod 15 = 0
            theDate = theDate.AddMinutes(1)
        Loop

        theDate = theDate.AddSeconds(-(theDate.Second)) 'Remove the seconds
        Return theDate
    End Function
End Module

Public Class AppointmentBLL
    ' This class formats and sends a meeting request via SMTP email

    Public StartDate As DateTime
    Public EndDate As DateTime
    Public Subject As String
    Public Summary As String
    Public Location As String
    Public AttendeeName As String
    Public AttendeeEmail As String
    Public OrganizerName As String
    Public OrganizerEmail As String

    Public Sub New(ByVal pdtStartDate As DateTime,
                    ByVal pdtEndDate As DateTime,
                    ByVal psSubject As String,
                    ByVal psSummary As String,
                    ByVal psLocation As String,
                    ByVal psAttendeeName As String,
                    ByVal psAttendeeEmail As String,
                    ByVal psOrganizerName As String,
                    ByVal psOrganizerEmail As String)

        ' Copy constructor parameters to public propeties

        StartDate = pdtStartDate
        EndDate = pdtEndDate
        Subject = psSubject
        Summary = psSummary
        Location = psLocation
        AttendeeName = psAttendeeName
        AttendeeEmail = psAttendeeEmail
        OrganizerName = psOrganizerName
        OrganizerEmail = psOrganizerEmail
    End Sub


    Public Sub EmailAppointment()

        ' Send the calendar message to the attendee

        Dim loMsg As New MailMessage
        Dim loTextView As AlternateView = Nothing
        Dim loHTMLView As AlternateView = Nothing
        Dim loCalendarView As AlternateView = Nothing
        Dim loSMTPServer As New SmtpClient("exchange.info.sys")

        ' SMTP settings set up in web.config such as:
        '  <system.net>
        '   <mailSettings>
        '    <smtp>
        '     <network
        '       host = "exchange.mycompany.com"
        '       port = "25"
        '       userName = "username"
        '       password="password" />
        '    </smtp>
        '   </mailSettings>
        '  </system.net>

        ' Set up the different mime types contained in the message
        Dim loTextType As System.Net.Mime.ContentType = New System.Net.Mime.ContentType("text/plain")
        Dim loHTMLType As System.Net.Mime.ContentType = New System.Net.Mime.ContentType("text/html")
        Dim loCalendarType As System.Net.Mime.ContentType = New System.Net.Mime.ContentType("text/calendar")

        ' Add parameters to the calendar header
        loCalendarType.Parameters.Add("method", "REQUEST")
        loCalendarType.Parameters.Add("name", "meeting.ics")

        ' Create message body parts
        loTextView = AlternateView.CreateAlternateViewFromString(BodyText(), loTextType)
        loMsg.AlternateViews.Add(loTextView)

        loHTMLView = AlternateView.CreateAlternateViewFromString(BodyHTML(), loHTMLType)
        loMsg.AlternateViews.Add(loHTMLView)

        loCalendarView = AlternateView.CreateAlternateViewFromString(VCalendar(), loCalendarType)
        loCalendarView.TransferEncoding = Net.Mime.TransferEncoding.SevenBit
        loMsg.AlternateViews.Add(loCalendarView)

        ' Adress the message

        loMsg.From = New MailAddress(OrganizerEmail)
        loMsg.To.Add(New MailAddress(AttendeeEmail))
        loMsg.Subject = Subject

        ' Send the message
        loSMTPServer.DeliveryMethod = SmtpDeliveryMethod.Network
        loSMTPServer.Send(loMsg)
    End Sub

    Public Function BodyText() As String

        ' Return the Body in text format

        Const BODY_TEXT =
            "Type:Single Meeting" & vbCrLf &
            "Organizer: {0}" & vbCrLf &
            "Start Time:{1}" & vbCrLf &
            "End Time:{2}" & vbCrLf &
            "Time Zone:{3}" & vbCrLf &
            "Location: {4}" & vbCrLf &
            vbCrLf &
            "*~*~*~*~*~*~*~*~*~*" & vbCrLf &
            vbCrLf &
            "{5}"

        Return String.Format(BODY_TEXT,
                            OrganizerName,
                            StartDate.ToLongDateString & " " & StartDate.ToLongTimeString,
                            EndDate.ToLongDateString & " " & EndDate.ToLongTimeString,
                            System.TimeZone.CurrentTimeZone.StandardName,
                            Location,
                            Summary)

    End Function

    Public Function BodyHTML() As String

        ' Return the Body in HTML format

        Const BODY_HTML =
                "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 3.2//EN"">" & vbCrLf &
                "<HTML>" & vbCrLf &
                "<HEAD>" & vbCrLf &
                "<META HTTP-EQUIV=""Content-Type"" CONTENT=""text/html; charset=utf-8"">" & vbCrLf &
                "<META NAME=""Generator"" CONTENT=""MS Exchange Server version 6.5.7652.24"">" & vbCrLf &
                "<TITLE>{0}</TITLE>" & vbCrLf &
                "</HEAD>" & vbCrLf &
                "<BODY>" & vbCrLf &
                "<!-- Converted from text/plain format -->" & vbCrLf &
                "<P><FONT SIZE=2>Type:Single Meeting<BR>" & vbCrLf &
                "Organizer:{1}<BR>" & vbCrLf &
                "Start Time:{2}<BR>" & vbCrLf &
                "End Time:{3}<BR>" & vbCrLf &
                "Time Zone:{4}<BR>" & vbCrLf &
                "Location:{5}<BR>" & vbCrLf &
                "<BR>" & vbCrLf &
                "*~*~*~*~*~*Generated by Server*~*~*~*~*~*<BR>" & vbCrLf &
                "<BR>" & vbCrLf &
                "{6}<BR>" & vbCrLf &
                "</FONT>" & vbCrLf &
                "</P>" & vbCrLf &
                vbCrLf &
                "</BODY>" & vbCrLf &
                "</HTML>"

        Return String.Format(BODY_HTML,
                            Summary,
                            OrganizerName,
                            StartDate.ToLongDateString & " " & StartDate.ToLongTimeString,
                            EndDate.ToLongDateString & " " & EndDate.ToLongTimeString,
                            System.TimeZone.CurrentTimeZone.StandardName,
                            Location,
                            Summary)

    End Function

    ' Reference to Microsoft ActiveX Data Objects 2.5 Library
    ' Reference to Microsoft CDO for Exchange 2000 Library
    ' Reference to Active DS Type Library

    ' Note: It is recommended that all input parameters be validated when they are
    ' first obtained from the user or user interface.
    Function GetFreeBusyString(ByVal strUserUPN As String, ByVal dtStartDate As Date,
                               ByVal dtEndDate As Date, ByVal Interval As Integer) As String

        Try
            ' Variables.
            'Dim iAddr As New CDO.Addressee()
            'Dim freebusy As String
            'Dim Info As New ActiveDs.ADSystemInfo()

            'iAddr.EmailAddress = strUserUPN
            'If Not iAddr.CheckName("LDAP://" & Info.DomainDNSName) Then
            'Throw New System.Exception("Error occured!")
            'End If

            ' Get the free/busy status in Interval minute intervals
            ' from dtStartDate to dtEndDate.
            'freebusy = iAddr.GetFreeBusy(dtStartDate, dtEndDate, Interval)
            'GetFreeBusyString = freebusy

        Catch err As Exception
            Console.WriteLine(err.ToString())
            GetFreeBusyString = ""
        End Try
    End Function

    Public Function VCalendar() As String

        ' Return the Calendar text in vCalendar format, compatible with most calendar programs

        Const lcDateFormat = "yyyyMMddTHHmmssZ"
        Dim loGUID As Guid = Guid.NewGuid  ' Or use the guid of an exiting meeting?

        Const VCAL_FILE = "BEGIN:VCALENDAR" & vbCrLf &
        "METHOD:REQUEST" & vbCrLf &
        "PRODID:Microsoft CDO for Microsoft Exchange" & vbCrLf &
        "VERSION:2.0" & vbCrLf &
        "BEGIN:VTIMEZONE" & vbCrLf &
        "TZID:(GMT-06.00) Central Time (US & Canada)" & vbCrLf &
        "X-MICROSOFT-CDO-TZID:11" & vbCrLf &
        "BEGIN:STANDARD" & vbCrLf &
        "DTSTART:16010101T020000" & vbCrLf &
        "TZOFFSETFROM:-0500" & vbCrLf &
        "TZOFFSETTO:-0600" & vbCrLf &
        "RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=11;BYDAY=1SU" & vbCrLf &
        "END:STANDARD" & vbCrLf &
        "BEGIN:DAYLIGHT" & vbCrLf &
        "DTSTART:16010101T020000" & vbCrLf &
        "TZOFFSETFROM:-0600" & vbCrLf &
        "TZOFFSETTO:-0500" & vbCrLf &
        "RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=3;BYDAY=2SU" & vbCrLf &
        "END:DAYLIGHT" & vbCrLf &
        "END:VTIMEZONE" & vbCrLf &
        "BEGIN:VEVENT" & vbCrLf &
        "DTSTAMP:{8}" & vbCrLf &
        "DTSTART:{0}" & vbCrLf &
        "SUMMARY:{7}" & vbCrLf &
        "UID:{5}" & vbCrLf &
        "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=""{9}"":MAILTO:{9}" & vbCrLf &
        "ACTION;RSVP=TRUE;CN=""{4}"":MAILTO:{4}" & vbCrLf &
        "ORGANIZER;CN=""{3}"":mailto:{4}" & vbCrLf &
        "LOCATION:{2}" & vbCrLf &
        "DTEND:{1}" & vbCrLf &
        "DESCRIPTION:{7}\N" & vbCrLf &
        "SEQUENCE:1" & vbCrLf &
        "PRIORITY:5" & vbCrLf &
        "CLASS:" & vbCrLf &
        "CREATED:{8}" & vbCrLf &
        "LAST-MODIFIED:{8}" & vbCrLf &
        "STATUS:CONFIRMED" & vbCrLf &
        "TRANSP:OPAQUE" & vbCrLf &
        "X-MICROSOFT-CDO-BUSYSTATUS:BUSY" & vbCrLf &
        "X-MICROSOFT-CDO-INSTTYPE:0" & vbCrLf &
        "X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY" & vbCrLf &
        "X-MICROSOFT-CDO-ALLDAYEVENT:FALSE" & vbCrLf &
        "X-MICROSOFT-CDO-IMPORTANCE:1" & vbCrLf &
        "X-MICROSOFT-CDO-OWNERAPPTID:-1" & vbCrLf &
        "X-MICROSOFT-CDO-ATTENDEE-CRITICAL-CHANGE:{8}" & vbCrLf &
        "X-MICROSOFT-CDO-OWNER-CRITICAL-CHANGE:{8}" & vbCrLf &
        "BEGIN:VALARM" & vbCrLf &
        "ACTION:DISPLAY" & vbCrLf &
        "DESCRIPTION:REMINDER" & vbCrLf &
        "TRIGGER;RELATED=START:-PT00H15M00S" & vbCrLf &
        "END:VALARM" & vbCrLf &
        "END:VEVENT" & vbCrLf &
        "END:VCALENDAR" & vbCrLf

        Return String.Format(VCAL_FILE,
                            StartDate.ToUniversalTime().ToString(lcDateFormat),
                            EndDate.ToUniversalTime().ToString(lcDateFormat),
                            Location,
                            OrganizerName,
                            OrganizerEmail,
                            "{" & loGUID.ToString() & "}",
                            Summary,
                            Subject,
                            Now.ToUniversalTime().ToString(lcDateFormat),
                            AttendeeEmail)
    End Function

End Class

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax