send email attachment error
Hi! I hope anyone of you can help me up in correcting my error as I've work on this email form for few days but my effort in debugging this form is in vain.
This is the code I get from other sources and have some changes but there is an error in :
strfilename = Path.GetFileName(FileInput.PostedFile.FileName)
the error description => System.NullReferenceException: Object reference not set to an instance of an object.
By the way, I've imported the System.web.mail and System.IO , change my HTMLINPUTFILE control to runat="server", encType="multipart/form-date" for form
Following is my email code :
-------------------------------------------------------------------------------------------------------------
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim msg As New MailMessage
msg.To = txtTo.Text
msg.From = txtFrom.Text
msg.Subject = txtSubject.Text
' msg.To = lstconsult.SelectedItem.Value
msg.Body = txtMsg.Text
msg.BodyFormat = MailFormat.Text
Dim strdir As String
Dim strfilename As String
strdir = "D:\\emailtemp\\"
strfilename = Path.GetFileName(FileInput.PostedFile.FileName)
FileInput.PostedFile.SaveAs(strdir + strfilename)
msg.Attachments.Add(New MailAttachment(strdir + strfilename))
Try
SmtpMail.Send(msg)
Catch ex As Exception
Response.Write(ex)
Finally
Response.Write("Your E-mail has been sent sucessfully")
End Try
' Uploaded file deleted after sending e-mail
File.Delete(strdir + strfilename)
End Sub
--------------------------------------------------------------------------------------------
I'll appreciate a lot if any of you can help me solve this problem..
Elena
|