Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / December 2005

Tip: Looking for answers? Try searching our database.

Sending Mails

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anuradha - 07 Dec 2005 12:34 GMT
Dear All
How can i send mails using vb.net
Thanx all
Venkat_KL - 07 Dec 2005 13:31 GMT
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.SelectedIndex)  '''-->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", "Manoj@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
Peter Proost - 07 Dec 2005 13:55 GMT
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)

> Sending Mail Using Dot net
>
[quoted text clipped - 251 lines]
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com
Søren Reinke - 07 Dec 2005 14:09 GMT
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

> Sending Mail Using Dot net
>
[quoted text clipped - 251 lines]
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com 
Venkat_KL - 07 Dec 2005 14:26 GMT
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 = "anuradhat@lankaequities.com"
                message.To = "venkat_kl@hotmail.com"
                message.Cc = "venkat.kl@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("BackupApplication","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
Anuradha - 08 Dec 2005 02:43 GMT
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

> One more sample:
>
[quoted text clipped - 42 lines]
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com
Herfried K. Wagner [MVP] - 07 Dec 2005 16:22 GMT
"Anuradha" <anuradhat@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'.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.