Important alert: (current site time 7/15/2013 8:31:12 AM EDT)
 

VB icon

_A simple class for Emailing UPDATED

Email
Submitted on: 12/11/2003 9:42:08 AM
By: Duane Warsham 
Level: Intermediate
User Rating: By 5 Users
Compatibility: VB.NET, ASP.NET
Views: 23881
(About the author)
 
     A .net class for emailing programmatically with attachments. Add to your project, then Dim object as SendMail to use it.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: _A simple class for Emailing UPDATED
// Description:A .net class for emailing programmatically with attachments. Add to your project, then Dim object as SendMail to use it.
// By: Duane Warsham
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1879&lngWId=10//for details.//**************************************

'* add the System.Web.Dll reference to your .Net 
'* project if doing a Windows Application. An 
'* ASP.Net app should include this reference.
'* Simply declare an object of type SendMail in code, ie in another class like a form class,
'Dim myEmail as New SendMail
'* set the to, from, attachment string path, body and subject properties, then call
'* MailMessage to send the email
Imports System.Web.Mail
Public Class SendMail
 Private _Subject As String
 Private _Body As String
 Private _Attachment as String
 Private _SendTo as String
 Private _From as String
 Public Property Subject() As String
 Get
 Return _Subject
 End Get
 Set(ByVal Value As String)
 _Subject = Value
 End Set
 End Property
 Public Property Body() As String
 Get
 Return _Body
 End Get
 Set(ByVal Value As String)
 _Body = Value
 End Set
 End Property
 Public Property Attachment() As String
 Get
 Return _Attachment
 End Get
 Set(ByVal Value As String)
 _Attachment = Value
 End Set
 End Property
 Public Property SendTo() As String
 Get
 Return _SendTo
 End Get
 Set(ByVal Value As String)
 _SendTo= Value
 End Set
 End Property
 Public Property From() As String
 Get
 Return _From
 End Get
 Set(ByVal Value As String)
 _From= Value
 End Set
 End Property
 Public Sub MailMessage()
 Dim objMail As New MailMessage
 Dim Attach as New MailAttachment(_Attachment)
 SmtpMail.SmtpServer = "" 'this needs to be the Smtp mail server, a proxy will not work, get this from your administrator, or Exchange server name in outlook
 'these Private variables can be set from the class you are using them in
 objMail.From = _From
 objMail.To = _SendTo
 objMail.Subject = _Subject 'Subject text
 objMail.Body = _Body 'Body Text
 objMail.Attachments.Add(Attach)
 objMail.BodyFormat = MailFormat.Html 'can be text also
 SmtpMail.Send(objMail)
 End Sub
End Class


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/8/2004 4:49:11 PM

Just learning and would like to know how do you "Simply declare an object of type SendMail in code"?
(If this comment was disrespectful, please report it.)

 
1/15/2004 1:43:17 PMD Warsham

To simply declare and use this class module, (Public Class SendMail), you just do this to use it in another form or module.
Dim x as New SendMail
Then you can use the properties and methods of the SendMail class.

(If this comment was disrespectful, please report it.)

 
4/19/2004 7:23:56 PM0C0DE

how do i retreive the "Smtp mail server"
(If this comment was disrespectful, please report it.)

 
11/1/2004 1:25:28 PM

The code is very good.
Thanks
(If this comment was disrespectful, please report it.)

 
1/1/2007 9:57:31 AMchristopher

this is good. i want project on ASP.NET please send my email id


christopher3_83@sify.com
(If this comment was disrespectful, please report it.)

 
1/26/2007 6:50:18 PMMike

Nice code
(If this comment was disrespectful, please report it.)

 
12/20/2007 9:12:50 AMKishan H. Hathiwala

nice and clean code but try to make async
(If this comment was disrespectful, please report it.)

 
9/9/2008 6:12:41 AMbalkrishna

It is fine code
(If this comment was disrespectful, please report it.)

 
2/28/2009 11:16:00 AMWalter Senekal

Hi
How do you Authenticate on the SMTP server?
Thanks Walter
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.