By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

Sending Mails

P: n/a
Dear All
How can i send mails using vb.net
Thanx all
Dec 7 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Sending Mail Using Dot net

System.Web.Mail
---------------
namespace provides the classes for sending Email in Dot net.

MailMessage
-----------
Manage the mail message contents.

Properties
==========

Attachment
-----------
Specifies the list of the attachments that are transmitted with the
message

Bcc
-----------
A list of semicolon delimited email addresses that receive a Blind
Carbon Copy of the message

Body
-----------
Contains the message text that has to be sent.

BodyEncoding
-------------
The encoding type of the email message.

BodyFormat
-----------
Defines the content type of the body of the message

Cc
-----------
A list of semicolon delimited email addresses that receive a Carbon Copy
of the message

From
-----------
The email address of the sender.

Header
-----------
Specifies the custom headers which are transmitted with the
Message

Priority
-----------
The priority of the email message

Subject
-----------
Subject Line of the email message.

To
-----------
email address of the recipient.

MailAttachments
----------------
Manage the mail attachment.

SmtpMail
-----------
Send email to the mail server.

Let us see it step by step
============================

Create a Visual basic application
And drop following controls and set the properties accordingly

Control Property
======== ==========

Label Text : Smtp Server

TextBox Name : txtSMTPServer

Label Text : From

TextBox Name : txtFrom

Label Text : From Display Name

TextBox Name : txtFromDisplayName

Label Text : Recipient

TextBox txtTo

Label Text : Attachment

ListBox Name : lstAttachment

Label Text : Subject

TextBox Name : txtSubject

Label Text : Message

TextBox Name : txtMessage
Multiline : True
Scrollbars : Both

Button Text : Add attachment
Name : BtnAdd
Button Text : Remove attachment
Name : btnRemove
Button Text : Send
Name : btnSend
CheckBox Text: Send As HTML
Name : chkFormat
OpenFileDialog Name : OFD
DefaultExt : *.*
InitialDirectory : c:\
Multiselect : true

Now let us see the coding part
==============================
'Invoke the Code widow and type the following statement above the Class
declaration

Imports System.Web.Mail '''-->Code

Within the Class declaration, in the general section declare variables
required for this project

' Variable which will send the mail

Dim obj As System.Web.Mail.SmtpMail '''-->Code
'Variable to store the attachments
Dim Attachment As System.Web.Mail.MailAttachment '''-->Code

'Variable to create the message to send
Dim Mailmsg As New System.Web.Mail.MailMessage() '''-->Code

'Double click on the addattachment button to add the code

'Type the following lines

'Show open dialogue box to select the files to attach

Dim Counter As Integer '''-->Code
OFD.CheckFileExists = True '''-->Code
OFD.Title = "Select file(s) to attach" '''-->Code
OFD.ShowDialog() '''-->Code

For Counter = 0 To UBound(OFD.FileNames) '''-->Code
lstAttachment.Items.Add(OFD.FileNames(Counter)) '''-->Code
Next '''-->Code

'Double Click on the Removeattachment button

'Type the following lines

'Remove the attachments
If lstAttachment.SelectedIndex > -1 Then '''-->Code
lstAttachment.Items.RemoveAt(lstAttachment.Selecte dIndex) '''-->Code
End If '''-->Code

'Double Click on the Send button

'Type the following lines
Dim Counter As Integer '''-->Code

'Validate the data
If txtSMTPServer.Text = "" Then '''-->Code
MsgBox("Enter the SMTP server info ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtFrom.Text = "" Then '''-->Code
MsgBox("Enter the From email address ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtTo.Text = "" Then '''-->Code
MsgBox("Enter the Recipient email address ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtSubject.Text = "" Then '''-->Code
MsgBox("Enter the Email subject ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

'Set the properties
‘Assign the SMTP server
obj.SmtpServer = txtSMTPServer.Text '''-->Code
'Multiple recepients can be specified using ; as the delimeter
‘Address of the recipient
Mailmsg.To = txtTo.Text '''-->Code
‘Your From Address
‘You can also use a custom header Reply-To for a different replyto
'address
Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text &
">" '''-->Code
'Specify the body format
If chkFormat.Checked = True Then '''-->Code
Mailmsg.BodyFormat = MailFormat.Html 'Send the mail in HTML Format
Else '''-->Code
Mailmsg.BodyFormat = MailFormat.Text '''-->Code
End If '''-->Code

'If you want you can add a reply to header
'Mailmsg.Headers.Add("Reply-To", "Ma***@geinetech.net")
'custom headersare added like this
'Mailmsg.Headers.Add("Manoj", "TestHeader")

‘Mail Subject
Mailmsg.Subject = txtSubject.Text '''-->Code

‘Attach the files one by one
For Counter = 0 To lstAttachment.Items.Count - 1 '''-->Code
Attachment = New MailAttachment(lstAttachment.Items(Counter))
‘Add it to the mail message
Mailmsg.Attachments.Add(Attachment) '''-->Code
Next '''-->Code

‘Mail Body
Mailmsg.Body = txtMessage.Text '''-->Code
‘Call the send method to send the mail
obj.Send(Mailmsg) '''-->Code

************************************************** *********************
Send SMTP mail using VB.NET
By Chris Dufour

http://www.codeproject.com/vb/net/epsendmail.asp

Bye
Venkat_KL

For Anything and Everything, Please Let Me Know

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Dec 7 '05 #2

P: n/a
Hi,

http://www.systemwebmail.net/

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Venkat_KL" <ve*******@yahoo.com> schreef in bericht
news:Ob**************@TK2MSFTNGP11.phx.gbl...
Sending Mail Using Dot net

System.Web.Mail
---------------
namespace provides the classes for sending Email in Dot net.

MailMessage
-----------
Manage the mail message contents.

Properties
==========

Attachment
-----------
Specifies the list of the attachments that are transmitted with the
message

Bcc
-----------
A list of semicolon delimited email addresses that receive a Blind
Carbon Copy of the message

Body
-----------
Contains the message text that has to be sent.

BodyEncoding
-------------
The encoding type of the email message.

BodyFormat
-----------
Defines the content type of the body of the message

Cc
-----------
A list of semicolon delimited email addresses that receive a Carbon Copy
of the message

From
-----------
The email address of the sender.

Header
-----------
Specifies the custom headers which are transmitted with the
Message

Priority
-----------
The priority of the email message

Subject
-----------
Subject Line of the email message.

To
-----------
email address of the recipient.

MailAttachments
----------------
Manage the mail attachment.

SmtpMail
-----------
Send email to the mail server.

Let us see it step by step
============================

Create a Visual basic application
And drop following controls and set the properties accordingly

Control Property
======== ==========

Label Text : Smtp Server

TextBox Name : txtSMTPServer

Label Text : From

TextBox Name : txtFrom

Label Text : From Display Name

TextBox Name : txtFromDisplayName

Label Text : Recipient

TextBox txtTo

Label Text : Attachment

ListBox Name : lstAttachment

Label Text : Subject

TextBox Name : txtSubject

Label Text : Message

TextBox Name : txtMessage
Multiline : True
Scrollbars : Both

Button Text : Add attachment
Name : BtnAdd
Button Text : Remove attachment
Name : btnRemove
Button Text : Send
Name : btnSend
CheckBox Text: Send As HTML
Name : chkFormat
OpenFileDialog Name : OFD
DefaultExt : *.*
InitialDirectory : c:\
Multiselect : true

Now let us see the coding part
==============================
'Invoke the Code widow and type the following statement above the Class
declaration

Imports System.Web.Mail '''-->Code

Within the Class declaration, in the general section declare variables
required for this project

' Variable which will send the mail

Dim obj As System.Web.Mail.SmtpMail '''-->Code
'Variable to store the attachments
Dim Attachment As System.Web.Mail.MailAttachment '''-->Code

'Variable to create the message to send
Dim Mailmsg As New System.Web.Mail.MailMessage() '''-->Code

'Double click on the addattachment button to add the code

'Type the following lines

'Show open dialogue box to select the files to attach

Dim Counter As Integer '''-->Code
OFD.CheckFileExists = True '''-->Code
OFD.Title = "Select file(s) to attach" '''-->Code
OFD.ShowDialog() '''-->Code

For Counter = 0 To UBound(OFD.FileNames) '''-->Code
lstAttachment.Items.Add(OFD.FileNames(Counter)) '''-->Code
Next '''-->Code

'Double Click on the Removeattachment button

'Type the following lines

'Remove the attachments
If lstAttachment.SelectedIndex > -1 Then '''-->Code
lstAttachment.Items.RemoveAt(lstAttachment.Selecte dIndex) '''-->Code
End If '''-->Code

'Double Click on the Send button

'Type the following lines
Dim Counter As Integer '''-->Code

'Validate the data
If txtSMTPServer.Text = "" Then '''-->Code
MsgBox("Enter the SMTP server info ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtFrom.Text = "" Then '''-->Code
MsgBox("Enter the From email address ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtTo.Text = "" Then '''-->Code
MsgBox("Enter the Recipient email address ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtSubject.Text = "" Then '''-->Code
MsgBox("Enter the Email subject ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

'Set the properties
'Assign the SMTP server
obj.SmtpServer = txtSMTPServer.Text '''-->Code
'Multiple recepients can be specified using ; as the delimeter
'Address of the recipient
Mailmsg.To = txtTo.Text '''-->Code
'Your From Address
'You can also use a custom header Reply-To for a different replyto
'address
Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text &
">" '''-->Code
'Specify the body format
If chkFormat.Checked = True Then '''-->Code
Mailmsg.BodyFormat = MailFormat.Html 'Send the mail in HTML Format
Else '''-->Code
Mailmsg.BodyFormat = MailFormat.Text '''-->Code
End If '''-->Code

'If you want you can add a reply to header
'Mailmsg.Headers.Add("Reply-To", "Ma***@geinetech.net")
'custom headersare added like this
'Mailmsg.Headers.Add("Manoj", "TestHeader")

'Mail Subject
Mailmsg.Subject = txtSubject.Text '''-->Code

'Attach the files one by one
For Counter = 0 To lstAttachment.Items.Count - 1 '''-->Code
Attachment = New MailAttachment(lstAttachment.Items(Counter))
'Add it to the mail message
Mailmsg.Attachments.Add(Attachment) '''-->Code
Next '''-->Code

'Mail Body
Mailmsg.Body = txtMessage.Text '''-->Code
'Call the send method to send the mail
obj.Send(Mailmsg) '''-->Code

************************************************** *********************
Send SMTP mail using VB.NET
By Chris Dufour

http://www.codeproject.com/vb/net/epsendmail.asp

Bye
Venkat_KL

For Anything and Everything, Please Let Me Know

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Dec 7 '05 #3

P: n/a
I use OpenSMTP (use google)

Because i need to send emails that include both text and HTML version in
same mail, with embeddet graphics.

/Søren Reinke

"Venkat_KL" <ve*******@yahoo.com> wrote in message
news:Ob****************@TK2MSFTNGP11.phx.gbl...
Sending Mail Using Dot net

System.Web.Mail
---------------
namespace provides the classes for sending Email in Dot net.

MailMessage
-----------
Manage the mail message contents.

Properties
==========

Attachment
-----------
Specifies the list of the attachments that are transmitted with the
message

Bcc
-----------
A list of semicolon delimited email addresses that receive a Blind
Carbon Copy of the message

Body
-----------
Contains the message text that has to be sent.

BodyEncoding
-------------
The encoding type of the email message.

BodyFormat
-----------
Defines the content type of the body of the message

Cc
-----------
A list of semicolon delimited email addresses that receive a Carbon Copy
of the message

From
-----------
The email address of the sender.

Header
-----------
Specifies the custom headers which are transmitted with the
Message

Priority
-----------
The priority of the email message

Subject
-----------
Subject Line of the email message.

To
-----------
email address of the recipient.

MailAttachments
----------------
Manage the mail attachment.

SmtpMail
-----------
Send email to the mail server.

Let us see it step by step
============================

Create a Visual basic application
And drop following controls and set the properties accordingly

Control Property
======== ==========

Label Text : Smtp Server

TextBox Name : txtSMTPServer

Label Text : From

TextBox Name : txtFrom

Label Text : From Display Name

TextBox Name : txtFromDisplayName

Label Text : Recipient

TextBox txtTo

Label Text : Attachment

ListBox Name : lstAttachment

Label Text : Subject

TextBox Name : txtSubject

Label Text : Message

TextBox Name : txtMessage
Multiline : True
Scrollbars : Both

Button Text : Add attachment
Name : BtnAdd
Button Text : Remove attachment
Name : btnRemove
Button Text : Send
Name : btnSend
CheckBox Text: Send As HTML
Name : chkFormat
OpenFileDialog Name : OFD
DefaultExt : *.*
InitialDirectory : c:\
Multiselect : true

Now let us see the coding part
==============================
'Invoke the Code widow and type the following statement above the Class
declaration

Imports System.Web.Mail '''-->Code

Within the Class declaration, in the general section declare variables
required for this project

' Variable which will send the mail

Dim obj As System.Web.Mail.SmtpMail '''-->Code
'Variable to store the attachments
Dim Attachment As System.Web.Mail.MailAttachment '''-->Code

'Variable to create the message to send
Dim Mailmsg As New System.Web.Mail.MailMessage() '''-->Code

'Double click on the addattachment button to add the code

'Type the following lines

'Show open dialogue box to select the files to attach

Dim Counter As Integer '''-->Code
OFD.CheckFileExists = True '''-->Code
OFD.Title = "Select file(s) to attach" '''-->Code
OFD.ShowDialog() '''-->Code

For Counter = 0 To UBound(OFD.FileNames) '''-->Code
lstAttachment.Items.Add(OFD.FileNames(Counter)) '''-->Code
Next '''-->Code

'Double Click on the Removeattachment button

'Type the following lines

'Remove the attachments
If lstAttachment.SelectedIndex > -1 Then '''-->Code
lstAttachment.Items.RemoveAt(lstAttachment.Selecte dIndex) '''-->Code
End If '''-->Code

'Double Click on the Send button

'Type the following lines
Dim Counter As Integer '''-->Code

'Validate the data
If txtSMTPServer.Text = "" Then '''-->Code
MsgBox("Enter the SMTP server info ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtFrom.Text = "" Then '''-->Code
MsgBox("Enter the From email address ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtTo.Text = "" Then '''-->Code
MsgBox("Enter the Recipient email address ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

If txtSubject.Text = "" Then '''-->Code
MsgBox("Enter the Email subject ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code

'Set the properties
'Assign the SMTP server
obj.SmtpServer = txtSMTPServer.Text '''-->Code
'Multiple recepients can be specified using ; as the delimeter
'Address of the recipient
Mailmsg.To = txtTo.Text '''-->Code
'Your From Address
'You can also use a custom header Reply-To for a different replyto
'address
Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text &
">" '''-->Code
'Specify the body format
If chkFormat.Checked = True Then '''-->Code
Mailmsg.BodyFormat = MailFormat.Html 'Send the mail in HTML Format
Else '''-->Code
Mailmsg.BodyFormat = MailFormat.Text '''-->Code
End If '''-->Code

'If you want you can add a reply to header
'Mailmsg.Headers.Add("Reply-To", "Ma***@geinetech.net")
'custom headersare added like this
'Mailmsg.Headers.Add("Manoj", "TestHeader")

'Mail Subject
Mailmsg.Subject = txtSubject.Text '''-->Code

'Attach the files one by one
For Counter = 0 To lstAttachment.Items.Count - 1 '''-->Code
Attachment = New MailAttachment(lstAttachment.Items(Counter))
'Add it to the mail message
Mailmsg.Attachments.Add(Attachment) '''-->Code
Next '''-->Code

'Mail Body
Mailmsg.Body = txtMessage.Text '''-->Code
'Call the send method to send the mail
obj.Send(Mailmsg) '''-->Code

************************************************** *********************
Send SMTP mail using VB.NET
By Chris Dufour

http://www.codeproject.com/vb/net/epsendmail.asp

Bye
Venkat_KL

For Anything and Everything, Please Let Me Know

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Dec 7 '05 #4

P: n/a
One more sample:

call the below procedure like this

SendMail("YES");

in a button click or any other event handler

'Dont forgrt to include the following namespace.
'using System.Web;
'using System.Web.Mail;
Public Shared Sub SendMail(ByVal success As String)
Try
' Construct a new mail message
Dim message As MailMessage = New MailMessage()
message.From = "an*******@lankaequities.com"
message.To = "ve*******@hotmail.com"
message.Cc = "ve*******@gmail.com"
message.Subject = "Hello from C# Winforms Application"
message.Body = "The backup started at: " + DateTime.Now + "\r and
ended "
+ success + " at :" + DateTime.Today.ToLongDateString() +
DateTime.Now
'if you want attach file with this mail, add the line below
'message.Attachments.Add(new MailAttachment("c:\\attach.txt",
'MailEncoding.Base64));

' Send the message
SmtpMail.Send(message)

Catch ex As Exception
System.Diagnostics.EventLog.WriteEnTry("BackupAppl ication","Error
occured while sending the
mail",System.Diagnostics.EventLogEnTryType.Error)
System.Console.WriteLine(ex.Message.ToString())
End Try

End Sub

For Anything and Everything, Please Let Me Know

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Dec 7 '05 #5

P: n/a
"Anuradha" <an*******@lankaequities.com> schrieb:
How can i send mails using vb.net


..NET 1.0/1.1: 'System.Web.Mail'.
..NET 2.0: 'System.Net.Mail'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Dec 7 '05 #6

P: n/a
Dear All

I tried below method to send mails but when the mail sends it gives an error
saying

Error MSG

====================================

An unhandled exception of type 'System.Web.HttpException' occurred in
system.web.dll

Additional information: Could not access 'CDO.Message' object.

=====================================

How can I get up from this error?

Thx
Anuradha

"Venkat_KL" <ve*******@yahoo.com> wrote in message
news:ew**************@TK2MSFTNGP12.phx.gbl...
One more sample:

call the below procedure like this

SendMail("YES");

in a button click or any other event handler

'Dont forgrt to include the following namespace.
'using System.Web;
'using System.Web.Mail;
Public Shared Sub SendMail(ByVal success As String)
Try
' Construct a new mail message
Dim message As MailMessage = New MailMessage()
message.From = "an*******@lankaequities.com"
message.To = "ve*******@hotmail.com"
message.Cc = "ve*******@gmail.com"
message.Subject = "Hello from C# Winforms Application"
message.Body = "The backup started at: " + DateTime.Now + "\r and
ended "
+ success + " at :" + DateTime.Today.ToLongDateString() +
DateTime.Now
'if you want attach file with this mail, add the line below
'message.Attachments.Add(new MailAttachment("c:\\attach.txt",
'MailEncoding.Base64));

' Send the message
SmtpMail.Send(message)

Catch ex As Exception
System.Diagnostics.EventLog.WriteEnTry("BackupAppl ication","Error
occured while sending the
mail",System.Diagnostics.EventLogEnTryType.Error)
System.Console.WriteLine(ex.Message.ToString())
End Try

End Sub

For Anything and Everything, Please Let Me Know

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Dec 8 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.