Important alert: (current site time 5/21/2013 1:21:29 PM EDT)
 

article

ASP.NET 2.0 Mail Example

Email
Submitted on: 1/25/2006 5:28:09 AM
By: Shawn Sturm  
Level: Intermediate
User Rating: By 2 Users
Compatibility: ASP.NET
Views: 16068
author picture
 
     System.Net.Mail in action... Just an example for those used to working with System.Web.Mail...

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article 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 article (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 article 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 article or article's description.
				
1<%@ Page Language="VB" %>
2
3<%@ Import Namespace="System.Net" %>
4<%@ Import Namespace="System.Net.Mail" %>
5
6<script runat="server">
7Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
8SendMail()
9End Sub
10
11Sub SendMail()
12Dim NetMail As New MailMessage
13Dim MailClient As New SmtpClient
14
15Dim ThisHost As String = "MAILSERVER"
16Dim ThisPort As Integer = 25
17Dim TheseCredentials As New NetworkCredential("USERNAME", "PASSWORD")
18
19Dim ThisSender As String = "SENDERNAME <SENDEREMAIL>"
20Dim ThisRecipient As String = "RECIPIENTEMAIL <RECIPIENTEMAIL>"
21Dim ThisSilentRecipient As String = String.Empty
22Try
23NetMail.From = New MailAddress(ThisSender)
24NetMail.ReplyTo = New MailAddress(ThisSender)
25NetMail.To.Add(New MailAddress(ThisRecipient))
26If Not ThisSilentRecipient = "" Then
27NetMail.Bcc.Add(New MailAddress(ThisSilentRecipient))
28End If
29NetMail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
30NetMail.IsBodyHtml = True
31NetMail.Priority = MailPriority.High
32NetMail.Subject = "Test Subject " & DateTime.Now.ToLongTimeString()
33NetMail.Body = "Test Body " & DateTime.Now.ToLongTimeString()
34
35MailClient.DeliveryMethod = SmtpDeliveryMethod.Network
36MailClient.Host = ThisHost
37MailClient.Port = ThisPort
38MailClient.UseDefaultCredentials = False
39MailClient.Credentials = TheseCredentials
40MailClient.Send(NetMail)
41Catch EXC As Exception
42Trace.Warn(EXC.Message)
43Finally
44NetMail.Dispose()
45NetMail = Nothing
46MailClient = Nothing
47End Try
48End Sub
49</script>


Other 3 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 article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/2/2007 12:36:36 PMriyaz

thank u....

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

 
6/15/2007 5:36:32 AMksncruz

Hi! Why do i get this error "Type 'MailMessage' is not defined". I only changed the Sender and the Recipient. Please help. Thanks!
(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 article, please click here instead.)
 

To post feedback, first please login.