ARTICLE

How to Send a Mail in your Application Without Using ComposeMail

Posted by Ravi Nagaraj Articles | ASP.NET using VB.NET March 24, 2011
Here you will see how to Send a Mail in your Application Without Using ComposeMail.
 
Reader Level:

How to Send a Mail in your Application Without Using ComposeMail (DYNAMICALLY FETCHING ALL DETAILS LIKE FROM EMAIL details and TOdetails and body details and regards details from ur application)

'in name space u have to import this
Imports System.Net.Mail

sendmail()

'sendmail definition details


Private Sub sendmail()
If MessageBox.Show("Do you want to send Email", "APPLICATION1", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

Imagine your storing Fromdetails to one of the table like

--> TblMailAccounts -table name containg all below information ->

pop3 incoming mail server--> u provide your mail server name like mymail.something.com
outgoing mail server--> destination server details

Code

Private Sub sendmail()
If MessageBox.Show("Do you want to send Email", "APPLICATION1", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

GstrSQL = "select * from TblMailAccounts where deletemode=0 "
Dim dtmail3 As New DataTable
dtmail3.Rows.Clear()
GDAL.DataTable(GstrSQL, dtmail3, GStrError)
Dim message As System.Net.Mail.MailMessage
Dim server As String
Dim port As Integer
Dim smailid As String
Dim pass As String
Dim i As Integer
Dim email100 As String
Dim strto As String
Dim bodymessage As String
Dim strendate As String
Dim dtsubject As New DataTable
Dim strsubject1 As String
dtsubject.Rows.Clear()

server = dtmail3.Rows(0)("OutgoingServer")
port = dtmail3.Rows(0)("PortNumber")
Dim smtp As New System.Net.Mail.SmtpClient(server, port)
smailid = dtmail3.Rows(0)("EmailAddress")

pass = dtmail3.Rows(0)("Password")

I am fetching subject details from below table

Table name :-> tblsubject

GstrSQL = "select systemtablevalue from tblsubject where systemtableid=102"
GDAL.DataTable(GstrSQL, dtsubject, GStrError)
If dtsubjact.Rows.Count > 0 Then
strsubject1 = dtsubjact.Rows(0)(0)
End If

Here I am storing subject details to one of the string called strsubject.

Dim strsubject As String = strsubject1

Now fetching regard details

table name:-> tblmailregards


Dim regards As String

Dim dtregards As New DataTable

GstrSQL = "select * from tblmailregards"
GDAL.DataTable(GstrSQL, dtregards, GStrError)

CurrRows = dtregards.Select(Nothing, Nothing, DataViewRowState.CurrentRows)
If CurrRows.Length <> 0 Then
For Each row In CurrRows
regards = row(0) & vbCrLf & row(1) & vbCrLf & row(2) & vbCrLf & row(3) &
vbCrLf & row(4) & vbCrLf & row(5)
Next
End If

Body details

In this body I am filling some item details to the customer

Dim strbody As String = "Dear sir/Madam, The item Enquiry Details" -> i done hard code
Dim strenq As String = "Enquiry No:"
Dim strdate As String = "Enquiry Date:"
Dim emtystring As String = ""
Dim itemnamestr As String = "Item Name:"
Dim quantitystr As String = "Item Quantity:"

In My application form I am taking enquiry number and date

strendate = strenq + txtInquiryNumber.Text & " " & " " & " " & " " & " " & strdate +
dtpInqDate.Text

Now I am fetching destination emailid with respect to vendorname present in my appliaction

For I = 0 To dgvVendors.Rows.Count - 1
Dim j As Integer
Dim strname As String = dgvVendors.Rows(i).Cells(0).Value
strto = getemail(strname)

(dgvVendors.Rows(i).Cells(0).Value -. in my form datagridview, fetching vendor name)

(strname containg -> customer name -> using customer name i am fetching email id from my contactdetail table-> that emailid i am storing into the string like strto)

If strto = "" Then

MessageBox.Show("Sorry!This vendor dont have Email id in Contact Book")

Exit Sub

End If


Dim stritemname As String
Dim strfinalquantity As String
Dim quantit As String
Dim strname123 As String
quantit = ""
strname123 = ""
stritemname = ""
strfinalquantity = ""



For j = 0 To DgInquiryDesc.Rows.Count - 1
strname123 = DgInquiryDesc.Rows(j).Cells(2).Value
quantit = DgInquiryDesc.Rows(j).Cells(6).Value
stritemname = stritemname & vbCrLf & strname123
strfinalquantity = strfinalquantity & vbCrLf & quantit

Next

bodymessage = emtystring & vbCrLf & " " & " " & strbody & vbCrLf & vbCrLf & vbCrLf & "
" & " " & " " & " " & " " & " " & " " & " " & strendate & vbCrLf & vbCrLf & vbCrLf &
itemnamestr & vbCrLf & stritemname & vbCrLf & vbCrLf & vbCrLf & vbCrLf & quantitystr
& vbCrLf & strfinalquantity & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf &
vbCrLf & regards

Body message output will come like this

smtp.EnableSsl = False
smtp.Credentials = New System.Net.NetworkCredential(smailid, pass)

message = New System.Net.Mail.MailMessage(smailid, strto, strsubject,
bodymessage)

smailid-> From email id
strto--> To Email id
strsubject--> Subject Details
bodymessage--> body Details


message.Body = bodymessage
smtp.Send(message)

Next

MessageBox.Show("Email sent success!", " Successful?", MessageBoxButtons.OK, MessageBoxIcon.Information)


End If

End Sub

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor