Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code which sucessfully attaches a document to an email and fires it to an end user, got two issues:

1. If no documents exist for the user, how can I skip it, so looking for ideas on how I can look up the table in question, if there are no entries then skip to just sending the email.

2. How do I attach multiple docs ? Do I need to read the table into an array ?

VB
Retrieve(Document)
        Dim fileName As String = String.Empty
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("TESTConnectionString").ConnectionString)
        Dim cmd As New SqlCommand("SELECT DocName,DocData FROM Docs WHERE Ext_Ref = " & strID, con)
        con.Open()
        Dim dReader As SqlDataReader = cmd.ExecuteReader()

        While dReader.Read()
            fileName = dReader("DocName").ToString()
            Dim documentBinary As Byte() = DirectCast(dReader("DocData"), Byte())
            Dim fStream As New FileStream(Server.MapPath("~\Docs") & "\" & fileName, FileMode.Create)
            fStream.Write(documentBinary, 0, documentBinary.Length)
            fStream.Close()
            fStream.Dispose()
        End While
        con.Close()
        mail.Attachments.Add(New Attachment("~Docs\" & fileName))
Posted

Inside the loop, just write the code to attach like below...
VB
While dReader.Read()
    fileName = dReader("DocName").ToString()
    Dim documentBinary As Byte() = DirectCast(dReader("DocData"), Byte())
    Dim fStream As New FileStream(Server.MapPath("~\Docs") & "\" & fileName, FileMode.Create)
    fStream.Write(documentBinary, 0, documentBinary.Length)
    fStream.Close()
    fStream.Dispose()

    ' If the file exists then attach.
    If !String.IsNullOrEmpty(fileName) Then 
        mail.Attachments.Add(New Attachment("~Docs\" & fileName))
End While

So,

  1. Here you first check whether FileName is not null or blank.
  2. Written the code to attach the file in the loop. So, all the files will be attached one by one.
 
Share this answer
 
VB
Try
    Dim SmtpClient As New SmtpClient("smtp.google.com") 'smtp server address
    Dim MailAddressCollection As New MailAddressCollection()
    Dim message As New MailMessage()
    message.From = New MailAddress("test@test.com", "test emailer")
    message.Body = "type body here"
    message.Subject = "type subject here"
    message.[To].Add("tested@golf.com") 'recipients email
    '    Dim att As New Attachment(New MemoryStream(bytes), name)
    Dim data As New Attachment("C:\Automated Extracts\document" & _
         DateTime.Today.ToString("yyyyMMdd") & ".zip")
    message.Attachments.Add(data)
    SmtpClient.Port = 25
    'SmtpClient.EnableSsl = True
    SmtpClient.Send(message)
Catch ex As Exception

End Try
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900