Sending E-mail from Web Form

OrlaLynch

New member
Joined
Feb 15, 2009
Messages
1
Programming Experience
1-3
Hi,

I am trying to send an e-mail from a web form. I am using Visual Basic in Visual Studio 2008. I am also using ASP.NET and a SQL Server 2005 database. I have found some code for this but it seems to be always for a Windows form and I keep coming across errors saying the code is "obsolete". Here is sample code I have been trying to use:


Imports System.Web.Mail

Partial Class QueryPage
Inherits System.Web.UI.Page

Dim objMail As New MailMessage()

Private Sub btnSend_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSend.Click

objMail.From = Me.txtFrom.Text.ToString
objMail.To = Me.txtTo.Text.ToString
objMail.Subject = Me.txtSubject.Text.ToString
objMail.Body = Me.txtMessage.Text.ToString
objMail.BodyFormat = MailFormat.Html
SmtpMail.Send(objMail)
Me.lblMessage.Text = "Email has been sent"

End Sub

End Class

I was confused when I started finding this so hard to do because I thought it would be a simple job. Any help would be greatly appreciated!

Thanks,
Orla
 
Yes System.Net.Mail...

Definitely .Net.Mail

here is a sample of HOW you COULD do it.

VB.NET:
            Dim Mail As New System.Net.Mail.MailMessage
            Dim strSender As New System.Net.Mail.MailAddress("Me@MyDomain.com")
            Mail.From = strSender

            Mail.To.Add("You@YourDomain.com")
               
            Mail.Subject = "Subject of the email."
            Mail.IsBodyHtml = True
            Mail.Body = "You can put your HTML content here or you don't have to use HTML content."

            Dim smtp As New System.Net.Mail.SmtpClient("Mail.MyDomain.Com", 25)
            smtp.Credentials = New System.Net.NetworkCredential("My Mail UserName", "My Mail Password")
            
            smtp.Send(Mail)

            Mail.Dispose()
            Mail = Nothing
            strSender = Nothing
            smtp = Nothing
 
Back
Top