Errors when sending email attachments from SQL

Discussion in 'ASP.NET / ASP.NET Core' started by TimCadieux, Oct 8, 2009.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Using the below code, I load the IDs of some photos which have been tagged for Download and loaded into a Session var. I then query the DB and attach the files before sending the email. This always works when I send 2 photos but always seems to not work at 3 photos. Instead of a proper email with attachments, I just get an e-mail with the word _Overflow_ in it. Any ideas would be appreciated.

    Code:
    Sub SendMail()
    
            Dim strFrom = "[email protected]"
            Dim strTo = Session("Download_Email")
            Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
            MailMsg.BodyEncoding = Encoding.Default
            MailMsg.Subject = "ACOImagery - Photos Tagged for download"
            MailMsg.Body = "The attached photos were tagged for download by " & Session("Download_Email")
            MailMsg.Priority = MailPriority.High
    
            Dim objConn As New SqlConnection(sConn)
            Dim strSql As String = ""
    
            Dim AddFilesToDownloadArray As ArrayList
            AddFilesToDownloadArray = New ArrayList
    
            strSql = " SELECT id_img, img_size, img_image, img_filename, exif_title FROM [img] "
    
            AddFilesToDownloadArray = Session("AddFilesToDownload")
    
            'Check to make sure an ID can only be added once!
            strSql += " WHERE "
            For x = 0 To AddFilesToDownloadArray.Count - 1
                strSql += " id_img = " & AddFilesToDownloadArray(x)
                If x < (AddFilesToDownloadArray.Count - 1) Then
                    strSql += " OR"
                End If
            Next
    
            strSql += " ORDER by id_img DESC"
    
            Dim objCommand As SqlCommand
            Dim objDataReader As SqlDataReader
            Dim objConnection As SqlConnection
    
            objConnection = New SqlConnection(sConn)
            objCommand = New SqlCommand(strSql, objConnection)
            Try
                If objConnection.State = ConnectionState.Closed Then
                    objConnection.Open()
    
                End If
                objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
    
                Do While objDataReader.Read
    
                    Dim strMem As System.IO.MemoryStream = New System.IO.MemoryStream(CType(objDataReader("img_image"), Byte()))
                    Dim strWriter As System.IO.StreamWriter = New System.IO.StreamWriter(strMem)
    
                    strWriter.Flush()
                    strMem.Position = 0
    
                    Dim attachment As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(strMem, objDataReader("img_filename"), "image/jpeg")
                    MailMsg.Attachments.Add(attachment)
    
                Loop
    
            Catch ex As Exception
                lblstatus.Text = ex.Message
            Finally
    
                objConnection.Close()
            End Try
    
            ' Close the database connection if it is still open.
            If objConnection.State = ConnectionState.Open Then
                objConnection.Close()
            End If
    
            MailMsg.IsBodyHtml = True
            'Smtpclient to send the mail message
            Dim SmtpMail As New SmtpClient
            SmtpMail.Host = "localhost"
            SmtpMail.Send(MailMsg)
    
        End Sub
    
    
    
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    are teh photos really large?

    it could have hit the attachment size limit.
     
  3. Less than 1mb each? I suppose this could be a possibility though, as I was testing locally with a free smtp, I hadn't thought of that.
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page