Follow by Email

Wednesday, 11 September 2013

Sending Appointments to an Outlook Calendar from an ASP.NET Web Site as at email attachement


Imports System.Net.Mail

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'INITIALIZING EVENT DETAILS

        Dim schLocation As String = "Conference Room"
        Dim schSubject As String = "Business visit discussion"
        Dim schDescription As String = "Schedule description"
        Dim schBeginDate As System.DateTime = Convert.ToDateTime("9/17/2013 10:00:00 PM")
        Dim schEndDate As System.DateTime = Convert.ToDateTime("9/17/2013 11:00:00 PM")

        'PUTTING THE EVENT DETAILS INTO AN ARRAY OF STRING

        Dim contents As [String]() = {"BEGIN:VCALENDAR", "PRODID:-//Flo Inc.//FloSoft//EN", "BEGIN:VEVENT", "DTSTART:" & schBeginDate.ToUniversalTime().ToString("yyyyMMdd\THHmmss\Z"), "DTEND:" & schEndDate.ToUniversalTime().ToString("yyyyMMdd\THHmmss\Z"), "LOCATION:" & schLocation, _
         "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" & schDescription, "SUMMARY:" & schSubject, "PRIORITY:3", "END:VEVENT", "END:VCALENDAR"}

        'THE METHOD 'WriteAllLines' CREATES A FILE IN THE SPECIFIED PATH WITH
        '       THE SPECIFIED NAME,WRITES THE ARRAY OF CONTENTS INTO THE FILE AND CLOSES THE
        '        FILE.SUPPOSE THE FILE ALREADY EXISTS IN THE SPECIFIED LOCATION,THE CONTENTS
        '       IN THE FILE ARE OVERWRITTEN

        System.IO.File.WriteAllLines(Server.MapPath("Sample.ics"), contents)

        'METHOD TO SEND EMAIL IS CALLED
        SendMail()



    End Sub

    Public Sub SendMail()
        'CONFIGURE BASIC CONTENTS OF AN EMAIL
        Dim mail As New MailMessage()
        Dim SmtpServer As New SmtpClient("smtp.gmail.com")
        mail.From = New MailAddress("EmailAddress")
        mail.[To].Add("EmailAddress")
        mail.Subject = "Calendar Event"
        mail.Body = "Calendar Event mail with attachment"

        'MAKE AN ATTACHMENT OUT OF THE .ICS FILE CREATED

        Dim mailAttachment As New Attachment(Server.MapPath("Sample.ics"))
        mail.Attachments.Add(mailAttachment)

        SmtpServer.Port = 587
        SmtpServer.Credentials = New System.Net.NetworkCredential("UserName", "Password")
        SmtpServer.EnableSsl = True

        SmtpServer.Send(mail)
    End Sub
End Class

0 comments:

Post a Comment


                                                            

 
Design by Abhinav Ranjan Sinha