Failing to send Attachments

partham

Active member
Joined
Dec 27, 2005
Messages
41
Location
Kolkata, India
Programming Experience
10+
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,

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
 
Dear Sir,My sincere gratitude for your time.The exception message is:"Invalid mail attachment 'C:\PPVS\Reports\PaymentVoucherReport\PaymentVoucherreport\bin\UsedVchReport20061014162955.XLS'." StringThe file is physically present at the stated location.The Stack Trace is:" at System.Web.Mail.MailAttachment.VerifyFile() at System.Web.Mail.MailAttachment..ctor(String filename, MailEncoding encoding) at EMAIL.SendAttachment(String strSender, String strRecipient, String strAttachmentFileName) in C:\Utilities\GeneralPurposeTools\EMAIL.vb:line 133" StringRegards,Partha.
 
Are you sure that that's the correct error message? Are you sure that it's not saying that the file is NOT physically present at the stated location? That would make more sense, and if I try to create a MailAttachment from a file that doesn't exist that's exactly the message I get. Look at the file path that the error message is telling you you're using. Does that file exist? This is the sort of elementary stuff you need to check yourself when an error occurs. If you're using a file path to do something and it fails the first thing to check is that the file path is valid.
 
Dear Sir,

Please pardon my ignorance.

However, in the attachment, please note that the file exists at the location.

Please advice.

Regards,
 

Attachments

  • untitled1.JPG
    untitled1.JPG
    104 KB · Views: 25
Last edited by a moderator:
OK, sorry, I thought that the bit about the file being physically located at that location was part of the error message. My mistake. All I can tell you is that if I create a MailAttachment from an XLS file path it works fine, but if I use an invalid path it gives me the very message you're getting. From your screen shot it does look like the file is there though. I'd suggest just adding this code before you create the attachment though, just to make sure:
VB.NET:
If IO.File.Exists(strAttachmentFileName) Then
    MessageBox.Show("The attachment file exists.")
Else
    MessageBox.Show("The attachment file does not exist.")
End If
 
Dear Sir,Thank you for your kind attention.I seem to have sorted out the problem. I have changed my file naming formula and made the file name smaller and it works. Is there is limitation on length of the Filename or FilePath.I am getting problem now with regards to the File Path. Some of the intermediate directories have long names. I think that could be the problem. However, I will update you after my tests.(The problem is that I have to deliver the software quickly)Regards,
 
There is a Windows-imposed limit on the length of paths but it's about 255 for a file name and 264 for a full path or something like that. It doesn't look like your path should have violated that limit.
 
Back
Top