Last post May 11, 2019 06:44 PM by KathyW
May 10, 2019 04:07 PM|simflex|LINK
I am getting the following error when attempting to email an uploaded file:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
A brief history:
We use a script (we got from aspsnippets.com).
This script allows you to attach multiple files. In my example, I was able to attach three files successfully.
Then I click Submit to send the attached files via email to user
However, when I click to Submit, I am getting the NullReferencException error.
I can confirm that when I check the Uploads folder where the files were stored, they are there.
I am not sure what the issue is.
Here is just relevant code that I am using:
<table style="width:90%;border:0; margin:auto;padding:10px;text-align:center;border-spacing:0;">
<td colspan="3"> </td>
<td><strong>File Attachments: </strong>
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
<table id = "attachedfiles"></table>
Dim msg As MailMessage = New MailMessage()
Dim address As MailAddress = New MailAddress("email@example.com")
msg.From = New MailAddress(email.Text.ToString())
msg.Subject = "Job Application from " & lblfname.Text & " " & lblmname.Text & " " & lbllname.Text
msg.Body = "Employment Application From " & lblfname.Text & " " & lblmname.Text & " " & lbllname.Text & "<br />Sent at:" & DateTime.Now & " <br />"
msg.Attachments.Add(New Attachment(New MemoryStream(bytes), lblfname.Text & " " & lblmname.Text & " " & lbllname.Text & DateTime.Now.Ticks & ".pdf"))
Dim files As List(Of HttpPostedFile) = DirectCast(Cache(Me.Key), List(Of HttpPostedFile))
For Each file As HttpPostedFile In files
msg.Attachments.Add(New Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType))
msg.IsBodyHtml = True
Dim smtp As New SmtpClient()
Dim client As SmtpClient = New SmtpClient()
client.Host = "18.104.22.168"
smtp.UseDefaultCredentials = False
'smtp.Credentials = NetworkCred
smtp.Port = 25
Any ideas what I am doing wrong?
Thanks in advance
May 11, 2019 04:12 PM|DA924|LINK
Well, you have to debug the code with the debugger and single-step the until it hits the line of code that is throwing the exception. The error message means thae code is trying to reference/use an object that is a null valued object that is not in memory.
You need to find out why.
OO is OO Java or .NET
May 11, 2019 06:44 PM|KathyW|LINK