Programming Tutorials

Comment on Tutorial - How to send email using VB.NET code By Issac



Comment Added by : Naresh

Comment Added at : 2011-05-24 07:27:12

Comment on Tutorial : How to send email using VB.NET code By Issac
Imports System.Web
Imports System.Net.Mail
Imports System.Text
Imports System.Web.Mail
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myMessage As New System.Web.Mail.MailMessage
TxtSendTo.Focus()
End Sub
Public Sub SendMailMultipleAttachments(ByVal From As String, _
ByVal sendTo As String, ByVal Subject As String, ByVal Body As String, Optional ByVal AttachmentFiles As ArrayList = Nothing, Optional ByVal CC As String = "", Optional ByVal BCC As String = "", Optional ByVal SMTPServer As String = "")
Dim i, iCnt As Integer
Dim myMessage As New System.Web.Mail.MailMessage
Try
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = System.Web.Mail.MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer

If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = BCC
If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(AttachmentFiles(i)) Then _
.Attachments.Add(AttachmentFiles(i))
Next
End If
End With
If SMTPServer <> "" Then _
'System.Web.Mail.SmtpMail.SmtpServer = "mail.mavensystems.biz"
System.Web.Mail.SmtpMail.SmtpServer = "smtp.comcast.net"
End If
System.Web.Mail.SmtpMail.Send(myMessage)
Catch myexp As Exception
MsgBox(myexp.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Sub

Private Function FileExists(ByVal FileFullPath As String) _
As Boolean
If Trim(FileFullPath) = "" Then Return False
Dim f As New IO.FileInfo(FileFullPath)
Return f.Exists
End Function

'Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
' With odlgAttachment
' .InitialDirectory = "C:\"
' .Filter = "All Files (*.*)|*.*|HTML Files (*.htm;*.html)|*.htm|Microsoft Mail Documents (*.msg)|*.msg|Word Documents (*.doc)|*.doc|Excel Files(*.xl*)|*.xl*|Excel Worksheets (*.xls)|*.xls|Excel Charts (*.xlc)|*.xlc|PowerPoint Presentations (*.ppt)|*.ppt|Text Files (*.txt)|*.txt"
' .FilterIndex = 1

' The OpenFileDialog control only has an Open button, not an OK button.
' However, there is no DialogResult.Open enum so use DialogResult.OK.
' If .ShowDialog() = DialogResult.OK Then
' Dim fname As String
' fname = .FileName
' myMessage.Attachments.Add(New System.Web.Mail.MailAttachment(fname))
' Dim strFileName() As String = .FileName.Split(New Char() {CChar("\")})
' strFileName.Reverse(strFileName)
' lblfile.Text = lblfile.Text & strFileName(0) & ","
' End If
' End With
'End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call SendMailMultipleAttachments(TxtFrom.Text, TxtSendTo.Text, TxtSub.Text, TxtBody.Text, , TxtCc.Text, TxtBcc.Text)
MsgBox("Your Messege Be Sent", MsgBoxStyle.Information, "Messege Sent")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'TxtFrom.Clear()
TxtSendTo.Clear()
TxtSub.Clear()
TxtBody.Clear()
TxtCc.Clear()
TxtBcc.Clear()
'lblfile.Clear()
TxtSendTo.Focus()
End Sub

'Private Sub Btn_Attach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Attach.Click
' Dim myMessage As New System.Web.Mail.MailMessage
' With odlgAttachment
' .InitialDirectory = "C:\"
' .Filter = "All Files (*.*)|*.*|HTML Files (*.htm;*.html)|*.htm|Microsoft Mail Documents (*.msg)|*.msg|Word Documents (*.doc)|*.doc|Excel Files(*.xl*)|*.xl*|Excel Worksheets (*.xls)|*.xls|Excel Charts (*.xlc)|*.xlc|PowerPoint Presentations (*.ppt)|*.ppt|Text Files (*.txt)|*.txt"
' .FilterIndex = 1

' ' The OpenFileDialog control only has an Open button, not an OK button.
' ' However, there is no DialogResult.Open enum so use DialogResult.OK.
' If .ShowDialog() = Windows.Forms.DialogResult.OK Then
' If IsNothing(TxtAttach) Then

' ' Clear the "(No Attachments)" default text in the ListView
' TxtAttach.Clear()
' End If
' 'TxtAttach.Add(New Attachment(.FileName))

' ' You only want to show the file name. The OpenFileDialog.FileName
' ' property contains the full path. So Split the path and reverse it
' ' to grab the first string in the array, which is just the FileName.
' Dim strFileName() As String = .FileName.Split(New Char() {CChar("\")})
' System.Array.Reverse(strFileName)
' 'TxtAttach.strFileName(0)
' End If
' End With

'End Sub
End Class


View Tutorial