User1216627406 posted
Greetings again,
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:
//HTML
<table style="width:90%;border:0; margin:auto;padding:10px;text-align:center;border-spacing:0;">
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="2"></td>
<td><strong>File Attachments: </strong>
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
</tr>
<tr>
<td></td>
<td>
<table id = "attachedfiles"></table>
</td>
</tr>
</table>
//VB:
Dim msg As MailMessage = New MailMessage()
Dim address As MailAddress = New MailAddress("myemail@gmail.com")
msg.To.Add(address)
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))
Next
msg.IsBodyHtml = True
Dim smtp As New SmtpClient()
Dim client As SmtpClient = New SmtpClient()
client.Host = "111.11.111.111"
smtp.UseDefaultCredentials = False
'smtp.Credentials = NetworkCred
smtp.Port = 25
client.Send(msg)
Any ideas what I am doing wrong?
Thanks in advance