Add attachment in Appointment sent through SMTP in VB.Net

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<pre class="prettyprint Hi I am using below code to send appointment using SMTP mail through System.Net.Mail. But it is not attaching attachment in the appointment I receive. Could anybody please see the below code find where I am wrong? Thanks in advance! Private Function SendAppointmentWithAttachment(objEventSess As tblEventSession) As Boolean
Dim objContextGSC As New ITQuarterlyModel.ITQuarterlyDataEntities
Dim objContextAD As New ITQuarterlyModel.ActiveDirectoryDataEntities
Dim sc As SmtpClient = New SmtpClient()
Dim userID As String = objEventSess.tblEvent.CreatedBy
Dim loginUserID As String = ViewData("LoggedOnUserId")
Dim objADUser As tblADUser = objContextAD.tblADUsers.Where(Function(fn) fn.sNetworkID.ToUpper = userID.ToUpper).FirstOrDefault
Dim objInvitee As tblInviteDetail = objContextGSC.tblInviteDetails.Where(Function(fn) fn.EventID = objEventSess.EventID And fn.UserID.ToUpper = loginUserID.ToUpper).FirstOrDefault()
Dim StDate As DateTime = objEventSess.BeginDateTime.ToUniversalTime Dates in UTC format
Dim EnDate As DateTime = objEventSess.EndDateTime.ToUniversalTime Dates in UTC Format
If objInvitee Is Nothing Then
Return False
End If
Try
sc.Port = ConfigurationManager.AppSettings("SMTPPortUsed").ToString
sc.Host = ConfigurationManager.AppSettings("SMTPServerName").ToString "SMTPGATE.GO.PPG.COM"
Dim msg As MailMessage = New MailMessage()
msg.From = New MailAddress(objADUser.sEmail, objADUser.sName)
msg.To.Add(New MailAddress(objInvitee.ToAddress, objInvitee.FirstName + " " + objInvitee.LastName))
msg.Subject = IIf(Not String.IsNullOrEmpty(objEventSess.Subject), objEventSess.Subject, "")
msg.Body = IIf(Not String.IsNullOrEmpty(objEventSess.Body), objEventSess.Body, "")
here to attach the meeting URL with Body if online meeting is selected
If objEventSess.IsOnlineMeeting Then
msg.Body += vbCrLf + "----------------------------------------------"
msg.Body += vbCrLf + "Join Online Meeting" + vbCrLf
msg.Body += objEventSess.MeetingURL
msg.Body += vbCrLf + "----------------------------------------------"
End If

Dim str As StringBuilder = New StringBuilder()
str.AppendLine("BEGIN:VCALENDAR")
str.AppendLine("PRODID:-XYZ")
str.AppendLine("VERSION:2.0")
If toCancel Then
str.AppendLine("METHOD:CANCEL")
Else
str.AppendLine("METHOD:REQUEST")
End If
str.AppendLine("X-MS-OLK-FORCEINSPECTOROPEN:TRUE")

str.AppendLine("BEGIN:VEVENT")
Dim ctx As New ITQuarterlyModel.ITQuarterlyDataEntities
Dim objDBAttachmentList As List(Of tblSessionAttachment) = ctx.tblSessionAttachments.Where(Function(fn) fn.SessionID = objEventSess.SessionID).ToList()
For Each objAtt In objDBAttachmentList
str.AppendLine("ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=" + objAtt.FileName + ":" + Convert.ToBase64String(objAtt.Attachment))
Next

str.AppendLine(String.Format("ATTENDEE;CN={0};RSVP=TRUE:mailto:{1}", msg.To, msg.To))
str.AppendLine(String.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", StDate))
str.AppendLine(String.Format("DTEND:{0:yyyyMMddTHHmmssZ}", EnDate))
str.AppendLine(String.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", StDate))
str.AppendLine("SEQUENCE:0")
str.AppendLine("LOCATION: " + IIf(String.IsNullOrEmpty(objEventSess.Location), objEventSess.Location, ""))
str.AppendLine(String.Format("UID:{0}", Guid.NewGuid()))
str.AppendLine("CLASS:PUBLIC")
str.AppendLine(String.Format("DESCRIPTION:" + ""))
str.AppendLine(String.Format("X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 3.2//EN><HTML><HEAD><META NAME=Generator CONTENT=MS Exchange Server version 14.02.5004.000><TITLE></TITLE></HEAD><BODY>" + msg.Body + "</BODY></HTML>"))
str.AppendLine(String.Format("X-ALT-DESC;FMTTYPE=text/html:" + msg.Body))
str.AppendLine(String.Format("SUMMARY:{0}", msg.Subject))
str.AppendLine(String.Format("ORGANIZER:MAILTO:{0}", msg.From.Address))
str.AppendLine("X-Microsoft-CDO-BUSYSTATUS:BUSY")
str.AppendLine("X-MICROSOFT-CDO-IMPORTANCE:1")
str.AppendLine("X-MICROSOFT-DISALLOW-COUNTER:FALSE")
str.AppendLine("X-MICROSOFT-CDO-IMPORTANCE:1")
str.AppendLine("BEGIN:VALARM")
str.AppendLine("TRIGGER:-PT15M")
str.AppendLine("ACTION:DISPLAY")
str.AppendLine("DESCRIPTION:Reminder")
str.AppendLine("END:VALARM")
str.AppendLine("END:VEVENT")
str.AppendLine("END:VCALENDAR")
Dim ct As System.Net.Mime.ContentType = New System.Net.Mime.ContentType("text/html") text/html
Dim avCal As AlternateView = AlternateView.CreateAlternateViewFromString(str.ToString(), ct)
msg.AlternateViews.Add(avCal)
msg.IsBodyHtml = True
msg.BodyEncoding = System.Text.Encoding.UTF8
sc.Send(msg)
Return True
Catch
Return False
End Try
End Function [/code]
<br/>

View the full article
 
Back
Top