Sending Mails

  • Thread starter Thread starter Anuradha
  • Start date Start date
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", "(e-mail address removed)")
'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
 
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
 
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 = "(e-mail address removed)"
message.To = "(e-mail address removed)"
message.Cc = "(e-mail address removed)"
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

S/MIME in vb.net 1
Lotus notes attachment opening return receipt 0
Configure smtp server in IIS7 on VISTA 1
Automate mail 1
Send Mail 7
Send Email 1
Access Send and Receive Mail thru Outlook using Access VBA 0
Send Mails 1

Back
Top