DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples > .Net

Discuss "Sending Emails Using ASP.NET" in the .Net forum.

.Net - Post your ASP.net and Windows Forms code samples here.


Reply « Previous Thread | Next Thread »  
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old March 23rd, 2008, 07:20 AM
richyrich's Avatar
Moderator

 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 325
Thanks: 22
Thanked 23 Times in 23 Posts
Rep Power: 1
richyrich is on a distinguished road

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default Sending Emails Using ASP.NET

This code sample is to show how to send emails in ASP.NET. Obviously there's lots more you can do, such as add attachments, read receipts etc. I'll add different code samples for these type of events.

Main Page
Code:
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="XHTML namespace">
<head runat="server">
<title>Sending Email using ASP.Net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lbl_response" runat="server"/><br/>
<br/>
To:
<asp:TextBox ID="txt_to" runat="server"/><br/>
cc:
<asp:TextBox ID="txt_cc" runat="server"/><br/>
bcc:
<asp:TextBox ID="txt_bcc" runat="server"/><br/>
Subject:
<asp:TextBox ID="txt_subject" runat="server"/><br/>
<br/>
Message:<br/>
<asp:TextBox ID="txt_mess" runat="server" TextMode="MultiLine"/><br/>
<br/>
<asp:Button ID="but_send" runat="server" Text="Send"/>
</form>
</body>
</html>
Code Behind
Code:
'import the net.mail namespace
Imports System.Net.Mail
 
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub but_send_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles but_send.Click
 
'create smtp instance
Dim smtp As New SmtpClient
 
'set the delivery method. We're using an external smtp server
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
 
'set the port the smtp server allows access on
smtp.Port = 25
 
'set the name of the smtp server to use
smtp.Host = "smtp.domain.com"
 
'create mail message instance
Dim myMail As New MailMessage()
 
'stringbuilder for building message body
Dim mail_body As New StringBuilder
 
'create html email body
mail_body.Append("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">")
mail_body.Append("<html>")
mail_body.Append("<head>")
mail_body.Append("<title>Send Email</title>")
mail_body.Append("<style type=""text/css"">")
mail_body.Append(".dblue8 { font-family:arial; color: #000066; font-size: 8pt }")
mail_body.Append("</style>")
mail_body.Append("</head>")
mail_body.Append("<body class=""dblue8"">")
'add in text from message box as the body
mail_body.Append(txt_mess.Text)
mail_body.Append("</body>")
mail_body.Append("</html>")
 
'create a variable to hold any error message
Dim err_mess As String = String.Empty
 
'create a try...catch area for creating the email and catching any errors
 Try
 
    'create instance of a mail address for the To address
    Dim mail_to As MailAddress
 
    'for multiple email addresses, split the email address by ;
    Dim arr_email As Array = Split(txt_to.Text, ";")
    'create a temp string to hold each email address in the to box
    Dim email_to As String
 
    'iterate through all the email addresses in the email array
         For Each email_to In arr_email
 
             'convert txt_to email to a mail address and set to variable
             mail_to = New MailAddress(Trim(email_to))
             'add the mail address to the .to property of the mail message
             myMail.To.Add(mail_to)
 
         Next
 
     'check if cc text box is empty
         If Not txt_cc.Text = String.Empty Then
             'txt_cc is not empty so need to add cc email addresses to cc property
             'create instance of a mail address for the cc address
             Dim mail_cc As MailAddress
             'for multiple email addresses, split the cc email addresses by ;
             'can use the same array instance as we used for the to addresses
             arr_email = Split(txt_cc.Text, ";")
             'create a temp string to hold each email address in the cc box
             Dim email_cc As String
             'iterate through all the email addresses in the email array
                  For Each email_cc In arr_email
                        'convert txt_to email to a mail address and set to variable
                        mail_cc = New MailAddress(Trim(email_cc))
                        'add the mail address to the .cc property of the mail message
                        myMail.CC.Add(mail_cc)
                  Next
         End If
 
     'check if bcc text box is empty
         If Not txt_bcc.Text = String.Empty Then
             'txt_bcc is not empty so need to add bcc email addresses to bcc property
             'create instance of a mail address for the bcc address
             Dim mail_bcc As MailAddress
             'for multiple email addresses, split the bcc email addresses by ;
             'can use the same array instance as we used for the to addresses
             arr_email = Split(txt_bcc.Text, ";")
             'create a temp string to hold each email address in the bcc box
             Dim email_bcc As String
             'iterate through all the email addresses in the email array
                 For Each email_bcc In arr_email
                     'convert txt_to email to a mail address and set to variable
                     mail_bcc = New MailAddress(Trim(email_bcc))
                     'add the mail address to the .bcc property of the mail message
                     myMail.Bcc.Add(mail_bcc)
                 Next
          End If
 
    'add your email as the .from property of the mail message
    myMail.From = New MailAddress("youremail@domain.com")
 
    'set the .subject property of the mail message to the value in the txt_subject box
    myMail.Subject = txt_subject.Text
 
    'set the body of the email to HTML. Set to False if you want Plain Text
    myMail.IsBodyHtml = True
 
    'set the .body property of the mail message to the email we created above
    myMail.Body = mail_body.ToString
 
    'send the email
    smtp.Send(myMail)
 
 Catch ex As Exception
         err_mess = ex.Message.ToString
 
 Finally
         myMail.Dispose()
 
 End Try
 
 If err_mess = String.Empty Then
          lbl_response.Text = "Your email has been sent"
 Else
          lbl_response.Text = err_mess
 End If
 
End Sub
 
End Class

Last edited by richyrich; April 2nd, 2008 at 09:19 AM.
Reply With Quote
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > Code Samples > .Net

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/net/57-sending-emails-using-active-server-pages-net.html
Posted By For Type Date
PolicyCheck - | Pensions | Investments | Savings | Advice This thread Refback April 2nd, 2008 09:31 AM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 10:32 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46