Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending e-mails with or without attachments using aspx

Status
Not open for further replies.
Jun 7, 2005
10
US
Below is the code used to allow users to send e-mails. Now I need to modifiy the code to allow users the ability to send emails with or without attachments. How do I do this?


<%@ import Namespace="System.Web.Mail" %>
<script runat="server">

Sub buttonSend_Click(sender As Object, e As EventArgs)
Dim emailMessage As MailMessage
emailMessage = New MailMessage
emailMessage.From = textEmail.Text
emailMessage.To = textTo.Text
emailMessage.Subject = textSubject.Text
emailMessage.Body = "Department: " & textDept.Text & vbcrlf & vbcrlf & "Name: " & textName.Text & vbcrlf & vbcrlf & "Url: " & textUrl.Text & vbcrlf & vbcrlf & "Message: " & textMessage.Text

SmtpMail.SmtpServer="cfmsvr1254"
Try
SmtpMail.Send(emailMessage)
labelStatus.Text = "Thank You!"
buttonSend.Enabled = False
Catch ex As Exception
labelStatus.Text = "Unable to send e-mail message."
End Try

End Sub

Sub buttonNewMessage_Click(sender As Object, e As EventArgs)
textEmail.Text = ""
textSubject.Text = ""
textDept.Text = ""
textName.Text = ""
textUrl.Text = ""
textMessage.Text = ""
buttonSend.Enabled = True
labelStatus.Text = ""
End Sub

</script>
 
You can use Attachments.Add to add an attachment. Look in your help files and it will tell you exactly how to do it.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Getting an error "Invalid mail attachment" when I modified the code below:
<%@ import Namespace="System.Web.Mail" %>
<%@ import Namespace="System.IO" %>
Sub buttonSend_Click(sender As Object, e As EventArgs)
SmtpMail.SmtpServer="server name goes here"
Dim emailMessage As MailMessage
Dim attachname as String 'string which stores the attachment name
emailMessage = New MailMessage
emailMessage.From = textEmail.Text
emailMessage.To = textTo.Text
emailMessage.Subject = textSubject.Text
emailMessage.Body = "Department: " & textDept.Text & vbcrlf & vbcrlf & "Name: " & textName.Text & vbcrlf & vbcrlf & "Url: " & textUrl.Text & vbcrlf & vbcrlf & "Message: " & textMessage.Text

'storing attachment name
attachname = txtFile.PostedFile.FileName
Dim myAttachment As MailAttachment = New MailAttachment(attachname)
emailMessage.Attachments.Add(myAttachment)

Try
SmtpMail.Send(emailMessage)
labelStatus.Text = "Thank You!"
buttonSend.Enabled = False
Catch ex As Exception
labelStatus.Text = "Unable to send e-mail message."
End Try

End Sub
 
Have you saved the posted file yet? If not it will be invalid so you would have to save it then pass the path of the saved file to tthe MailAttachment.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Not sure what you mean by posted the file yet. When I click on the browse button it gets the file from my c drive.
 
What I mean is have you saved the posted file yet? e.g.
Code:
attachname.PostedFile.SaveAs(SavePathGoesHere)

You can only attach a file that your web server has access to and until you have saved the file, it is still on your hard drive and the web server doesn't have access to it.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Yes. I moved the code into the try and now I just get the "Unable to send e-mail" message.

Try
'storing attachment name
attachname = txtFile.PostedFile.FileName
'getting the file name and storing it in the server
' path and then attaching it to the 'mail.
If (attachname <> "") then
attachname = attachname.substring
(attachname.lastindexof("\")+1)
attachname.PostedFile.saveAs(Server.MapPath
(attachname))
myAttachment = new mailattachment(Server.MapPath
(attachname.Request.Path))
emailMessage.Attachments.Add(myAttachment)
End if

SmtpMail.Send(emailMessage)
labelStatus.Text = "Thank You!"
buttonSend.Enabled = False
Catch ex As Exception
labelStatus.Text = "Unable to send e-mail message."
End Try
 
Which line is the exception thrown on and what do ex.Message and ex.InnerException say?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
The code fails on:
myAttachment = new mailattachment(Server.MapPath
(attachname.Request.Path))

ex.message = "Unable to send e-mail message."
 
I forgot to add this :
Compiler Error Message: BC30456: 'Request' is not a member of 'String'.
 
I found my problem.
code causing problem:
myAttachment = new mailattachment(Server.MapPath
(attachname.Request.Path))
Removed the .request.path
myAttachment = new mailattachment(Server.MapPath
(attachname))
I can now send an email with or without attachments.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top