Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Email.Attributes("type") = "email"
End Sub

Protected Sub ContactUsclick()
Try
If ctrlGoogleReCaptcha.Validate Then
Dim ID = System.Guid.NewGuid
Dim FullNameText = Trim(FullName.Value)
Dim CompanyNameText = Trim(CompanyName.Value)
Dim EmailText = Trim(Email.Value)
Dim PhoneText = Phone.Value
Dim SerialNumberText = SerialNumber.Value
Dim ProductLocationAddress = ProductLocation.Value
Dim CommentText = Trim(Comment.Value)

DataAccessLayer.DataAccess.InsertServiceRequest(ID, FullNameText, CompanyNameText, EmailText, _
PhoneText, SerialNumberText, ProductLocationAddress, CommentText)

Dim AttachmentList As New List(Of Attachment)

For Each UploadedFile As HttpPostedFile In FileUpload.PostedFiles
    Dim ServiceQueueUniqueIdentifier = System.Guid.NewGuid
    Dim strFileName As String = System.IO.Path.GetFileName(UploadedFile.FileName)
    Dim attachment As New Attachment(UploadedFile.InputStream, strFileName)
    AttachmentList.Add(attachment)

    Dim fs As Stream = UploadedFile.InputStream
    Dim br As New BinaryReader(fs)
'Dim bytes As Byte() = br.ReadBytes(fs.Length)
    DataAccessLayer.DataAccess.InsertServiceRequestAttachments(ServiceQueueUniqueIdentifier, ID, UploadedFile.FileName, UploadedFile.ContentType, br.ReadBytes(fs.Length))

Next


Dim body As String = BusinessLogic.Utilities.MailingFunctions.FormatServiceRequestToXML(FullNameText, CompanyNameText, EmailText, PhoneText, _
SerialNumberText, ProductLocationAddress, CommentText, AttachmentList.Count)

BusinessLogic.Utilities.MailingFunctions.SendSMTPContactRequest("""Phiston Feedback Mailer""" & "<" & ConfigurationManager.AppSettings("ContactUSFromEmailAddress") & ">", _
"Shahrouz.ebadian@gmail.com", "Service Request Received", body, AttachmentList, Nothing)

'BusinessLogic.Utilities.MailingFunctions.SendSMTPContactRequest("""Phiston Feedback Mailer""" & "<" & ConfigurationManager.AppSettings("ContactUSFromEmailAddress") & ">", _
'                                          ConfigurationManager.AppSettings("ServiceContactToEmailAddress"), "Service Request Received", body, AttachmentList, Nothing)

Response.Redirect("contactthanks.aspx")
Else
ErrorLbl.Visible = True
End If
Catch ex As Exception
Throw
End Try
End Sub


Public Shared Function FormatServiceRequestToXML(ByVal FullName As String, ByVal CompanyName As String, ByVal Email As String, ByVal Phone As String, _
ByVal Serial As String, ByVal ProductAddress As String, ByVal comment As String, ByVal AttachmentCount As Integer)
Try

Dim builder As StringBuilder = New StringBuilder
Dim body As String = ""

builder.Append("<table bgcolor='#33ffff' width='100%' border='1' cellpadding='0' cellspacing='0'>")
builder.Append("<tr>")
builder.Append("<td width='100%' align='center' colspan='2' bgcolor='#00bbff'>")
builder.Append("<B>Phiston Contact Us</B>")
builder.Append("</td>")
builder.Append("</tr>")


builder.Append("<tr>")
builder.Append("<td style='width: 20%'><B>Date</B></td>")
builder.Append("<td style='width: 80%'>&nbsp;")
builder.Append(Now().ToLongDateString & " " & Now().ToLongTimeString)
builder.Append("</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%'><B>DB Name</B></td>")
builder.Append("<td style='width: 80%'>&nbsp;")
builder.Append(BusinessLogic.SystemInfo.SystemInformation.GetDBName)
builder.Append("</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 100%' align='center' colspan='2' bgcolor='#00bbff'><B>Information</B></td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Full Name</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & FullName & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Company Name</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & CompanyName & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Email</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & Email & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Phone</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & Phone & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Serial #</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & Serial & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Product Address</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & ProductAddress & "</td>")
builder.Append("</tr>")

builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Comment</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & comment & "</td>")
builder.Append("</tr>")


builder.Append("<tr>")
builder.Append("<td style='width: 20%' align='left'><B>Files Attached</B></td>")
builder.Append("<td style='width: 80%' align='left'>" & AttachmentCount & "</td>")
builder.Append("</tr>")

Return builder.ToString

Catch e As Exception
Throw
End Try

End Function

Public Shared Sub SendSMTPContactRequest(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String, _
ByVal Attachments As List(Of Attachment), ByVal CC As String)

Try
Dim insMail As New MailMessage(New MailAddress(strFrom), New MailAddress(strTo))
insMail.Priority = MailPriority.High
insMail.IsBodyHtml = True
With insMail
.Subject = strSubject
.Body = strBody
If Not (IsNothing(CC)) Then
.CC.Add(New MailAddress(CC))
End If

If Not IsNothing(Attachments) AndAlso Attachments.Count <> 0 Then
For i As Integer = 0 To Attachments.Count - 1
.Attachments.Add(Attachments(i))
Next
End If


End With
Dim smtp As New System.Net.Mail.SmtpClient

smtp.EnableSsl = False
smtp.Port = ConfigurationManager.AppSettings("SmtpPort")
smtp.Host = ConfigurationManager.AppSettings("SmtpHost")
smtp.Credentials = New System.Net.NetworkCredential(ConfigurationManager.AppSettings("ContactUsFromEmailAddress"), ConfigurationManager.AppSettings("ContactUSFromEmailPassword"))
smtp.Send(insMail)

Catch e As Exception
Throw
End Try
End Sub
Posted
Updated 20-Oct-15 9:27am
v3
Comments
Richard Deeming 20-Oct-15 10:43am    
So not only are we supposed to guess what error the line produces, we're also supposed to guess the content of your custom method which produces it?

Use the "Improve question" button to update your question with the missing details. Include the full details of the error you're getting, and the source code for the methods you're calling.
Malikdanish 20-Oct-15 14:43pm    
Thanks for response now that is the issue ..
So the code is for a webpage where a user UploadsFiles
I want to take the uploaded Files an do 2 things
1) Save files to Database
2) Email Files
The problem is I cant do both and let me explain why
This one line of code is causing a problem
DataAccessLayer.DataAccess.InsertServiceRequestAttachments(ServiceQueueUniqueIdentifier, ID, UploadedFile.FileName, UploadedFile.ContentType, br.ReadBytes(fs.Length))
That is the line that inserts the files into the database
that part works..
it inserts the data into the database no problem.. BUT
when that line is there when I get the emails the attachments are blank
If I take that line out... I get the email with the attachments no problem
make sense?
If I omit that line I get the attachments fine in the email
If i add the line. It saves to Database no problem but email has issues
So not sure why but something on that line is causing my email attachments to get cleared
Richard Deeming 20-Oct-15 14:54pm    
You still haven't told us what the error message is, or provided the missing code.

We can't see your screen, access your computer, or read your mind. If you don't show us your code, we can't help you.
Malikdanish 20-Oct-15 15:06pm    
My Dear I am working remotely why i hide my code i also just made this calling method and the others methods are in build into our enterprise app they are working on other pages , and the error i already mention the line which create error the error is that when i add this line into my method it save the file into the db but does not send it via email attachment and when i omit or remove this line the email does have attachment but the file does not saved so instead of two one operation performed if i remove then line then attachment goes
Richard Deeming 20-Oct-15 15:08pm    
If we can't see your code, we can't help you.

1 solution

You read the bytes from the stream to save in the database. That sets the current position of the stream to the end.

When the attachment tries to read the content from the stream, it reads zero bytes, because there is no more data to read. As a result, you get empty attachments.

You need to reset the stream's position after you've read it:
VB.NET
Dim fs As Stream = UploadedFile.InputStream
Dim br As New BinaryReader(fs)
DataAccessLayer.DataAccess.InsertServiceRequestAttachments(ServiceQueueUniqueIdentifier, ID, UploadedFile.FileName, UploadedFile.ContentType, br.ReadBytes(fs.Length))

' Add this line:
fs.Position = 0
 
Share this answer
 
Comments
Malikdanish 20-Oct-15 15:42pm    
Thanks Richard Deeming I am checking and will inform you about Thanks Thanks for devoting your time and effort Appreciatable
Malikdanish 20-Oct-15 15:49pm    
After which line should i add this line of code ? Richard Deeming
Richard Deeming 20-Oct-15 15:51pm    
Inside the For Each UploadedFile As HttpPostedFile In FileUpload.PostedFiles loop in the ContactUsclick method.

I've included three lines from your current code; you just need to add the last line immediately after them.
Malikdanish 21-Oct-15 1:24am    
Thanks u did great job Deeming
Malikdanish 20-Oct-15 15:55pm    
Ok Richard I am on testing and will inform you .. Thanks alot

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