Coding dan IT

The SMTP email sending. [asp.net using vb]

hello all.
i have read before about sending email through smtp in asp.net, though it sounds easy, but i’ve hit some configuration error or something else. here i will share you step by step how to send email, and some problem that i’ve met and how i solved it.
1. The SMTP configuration at web.config
you will have web.config file in your folder. open it, and you will see xml modelled settings. it will looks like this

<configuration>
<!– Add the email settings to the <system.net> element –>
<system.net>
<mailSettings>
<smtp>
<network
host=”localhost”
port=”25″
userName=”user@domain.com”
password=”yourpasswordofthisuser” />
</smtp>
</mailSettings>
</system.net>

</configuration>

there are 4 most important item. see above code. first is server, this is your server where you host your site. second is the mail host. usually 25, or in some hosting, 26.  then username and password is the username and password you use to login to your site’s email.
after this step finished. next is to code your application
2. code behind to send
you will need 1 reference in your class. which is

Imports System.Net.Mail

that class is modified in .net 2.0 and above. with that , you will be able to use the smtp class. this is how the code should looks like

Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
'!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
Const ToAddress As String = "you@youremail.com"

‘(1) Create the MailMessage instance
Dim mm As New MailMessage(UsersEmail.Text, ToAddress)
‘(2) Assign the MailMessage’s properties
mm.Subject = Subject.Text
mm.Body = Body.Text
mm.IsBodyHtml = False
‘(3) Create the SmtpClient object
Dim smtp As New SmtpClient
‘(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(mm)
End Sub

that button event code is the one originally written by Scott Mitchell in his great articles sending email with asp.net 2.0 . i have used his method and it worked.
next thing to do is to upload your website to localhost, or to upload directly to your hosting. and thats it. you will have a new email at the destination folder.
3. some problem that may arise
#1 the failure sending mail : when i hit this, i recheck my port and its wrongly typed. my hosting need 26, where i key in 25.
i think that should be enough. anyone who has problem may comment here, and i will try to help if i can. thanks for reading.
PS: thanks a lot for scott mitchell
Satu CL
.net and php software engineer

Protecsted Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
'!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
Const ToAddress As String = "you@youremail.com"

‘(1) Create the MailMessage instance
Dim mm As New MailMessage(UsersEmail.Text, ToAddress)
‘(2) Assign the MailMessage’s properties
mm.Subject = Subject.Text
mm.Body = Body.Text
mm.IsBodyHtml = False
‘(3) Create the SmtpClient object
Dim smtp As New SmtpClient
‘(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(mm)
End Sub