partham
Active member
Dear Sir,
I need to send files as attachment along with a mail. I have used the following code for the same. However, the code always fails when the attachment object os being created.
This is to request you to kindly let me know what is the reason for the same.
Thanks in advance.
Regards,
I need to send files as attachment along with a mail. I have used the following code for the same. However, the code always fails when the attachment object os being created.
This is to request you to kindly let me know what is the reason for the same.
Thanks in advance.
Regards,
VB.NET:
Public Shared Sub SendAttachment(ByVal strSender As String, ByVal strRecipient As String, ByVal strAttachmentFileName As String)
Dim l_objSMTPMail As System.Web.Mail.SmtpMail
Dim l_objMessage As System.Web.Mail.MailMessage
Dim l_objAttachment As System.Web.Mail.MailAttachment
Const ROUTINE_NAME As String = "SendAttachment"
If Not (f_objLogger Is Nothing) Then f_objLogger.LogRoutineEntry(MODULE_NAME, ROUTINE_NAME)
Try
'Validate the Input
strSender = AssignNotNullStringValue(strSender)
strRecipient = AssignNotNullStringValue(strRecipient)
strAttachmentFileName = AssignNotNullStringValue(strAttachmentFileName)
'Check if the Attachment File exists
If System.IO.File.Exists(strAttachmentFileName) Then
'Set the Server
SMTPServerName = ""
'Create the Attachment
'l_objAttachment = New System.Web.Mail.MailAttachment(strAttachmentFileName, Web.Mail.MailEncoding.UUEncode)
'Create the Message
l_objMessage = New System.Web.Mail.MailMessage
l_objMessage.From = strSender
l_objMessage.To = strRecipient
l_objMessage.Subject = FileSystemTool.ExtractFileName(strAttachmentFileName)
'l_objMessage.Attachments.Add(l_objAttachment)
'Send the Mail
l_objSMTPMail.SmtpServer = f_strSMTPServerName
l_objSMTPMail.Send(l_objMessage)
Else
Throw New FileNotFoundException(strAttachmentFileName)
End If
Catch ex As Exception
Throw ex
End Try
If Not (f_objLogger Is Nothing) Then f_objLogger.LogRoutineExit(MODULE_NAME, ROUTINE_NAME)
End Sub