How To Send Email Message In .NET

May 14, 2008 – 6:04 am

Last day i see some question in one best forum for programming that want to know how to send an email using .NET.
I have some experience when i work in one dutch project (’ TennisSite ‘) by Wellfound and have a module for send an email for customer.

So i will explain it here, about how to send an email using .NET. We use System.Net.Mail namespace for this project.

Ok this is the code snippet for send an email using .NET

Dim strUserName As String = "" ‘Username for smtpclient
Dim strPassword As String = "" ‘user password for smtpclient
Dim strHostName As String = "" ’smtpaddress
Dim intPort As Integer = 25
Dim credentials As New System.Net.NetworkCredential(strUserName, strPassword)
Dim message As New MailMessage()
Dim emailClient As New SmtpClient(strHostName, intPort)

With message
       .From = New MailAddress(strUserName)
       .To.Add(txtSendTo.Text.Trim)
       .Subject = "Test Send Email"
       .Body = txtMessage.Text.Trim
       .IsBodyHtml = True
       .Attachments.Add(New Attachment("path of file")
       .Bcc.Add("email address")
       .CC.Add("email address")
End With

emailClient.Credentials = credentials

Try
        emailClient.Send(message)
Catch ex As Exception
        MsgBox(ex.Source & " : " & ex.Message, MsgBoxStyle.Exclamation, "Error Message")
Finally
        message = Nothing
        emailClient = Nothing
End Try

I hope this article will help you in your some application project for send an email in future.


Simple Application Using .NET Part I

May 14, 2008 – 3:29 am

On this article, i will share my knowladge about how to make a simple application "CRUD" (Create, Read, Update, Delete) in .NET. One days ago, i get a request from some one that want to learn about .NET application using ADO.NET

For this part i give a simple application that using MS Access as a database application. We using System.Data.OleDb namespace for this.

This is the code snippet for this simple application:

Try
    Using Conn As New OleDbConnection(connString)
            Conn.Open()
            Using sqlCommand As New OleDbCommand()
             With sqlCommand
                   .CommandType = CommandType.Text
                   .CommandText = _stringSQL
                   .Connection = Conn
                   .ExecuteNonQuery()
             End With
            End Using
    End Using
    Return True
Catch ex As Exception
    MsgBox(ex.Source & " : " & ex.Message, MsgBoxStyle.Exclamation, "Error Messages")
    Return False
End Try

This metode we use for execute the sql command that given in INSERT, UPDATE and DELETE command.
Then when we want to bind database field or READ from database, we use OleDbDataAdapter and fill it to DataTable

Try
     Using Conn As New OleDbConnection(connString)
             Using daResult As New OleDbDataAdapter(_stringSQL, Conn)
                     daResult.Fill(_dtResult)
             End Using
     End Using
Catch ex As Exception
     MsgBox(ex.Source & " : " & ex.Message, MsgBoxStyle.Exclamation, "Error Messages")
End Try

For detail you can download SimpleApplication source to learn more from there.
I hope this article can help everyone who want to learn .NET espesially in database application.