Send email using ASP.NET

Microsoft ASP.Net has built-in classes for sending email. See http://msdn.microsoft.com/en-us/library/system.net.mail.aspx for full details.
Here's the sampe code - just copy into Notepad and save as something like sendmail.aspx
This code will contact your SMTP server, authenticate using your email server's username and password, then send the email. (Works with ASP.Net 2.0 and higher)


<%@ Page Language="vb" %>
<html>
<Script Language="VB" RunAt="Server">
    'NOTES:
    '   *** Replace me@mycompany.com with your actual email address
    '   *** Replace mail.mycompany.com with your actual SMTP server
    '   *** Replace username and password with your actual SMTP server credentials to send using SMTP

   
    Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs)
     
    End Sub
    Sub btn_send_Click(ByVal Sender As Object, ByVal e As EventArgs)
        Try
            'Declare our variables here...
            Dim emailMsg As New System.Net.Mail.MailMessage
            Dim smtp As New System.Net.Mail.SmtpClient("mail.mycompany.com", 25) '25=port 25
           
            '1) set the email addresses
            emailMsg.From = New System.Net.Mail.MailAddress("me@mycompany.com")
            emailMsg.To.Add(txt_emailTo.Text)
           
            '2) set the email content
            emailMsg.IsBodyHtml = True 'let's make this email nice HTML format rather than plain text
            emailMsg.Subject = "Wow - this script worked"
            emailMsg.Body = "<b>Wow</b> - I just sent this email using ASP.Net"
            emailMsg.Body &= "<br>(I did it all on my own without any help)"
           
            '3) set the SMTP server credentials
            smtp.Credentials = New Net.NetworkCredential("username", "password")
           
            '4) send the email message
            smtp.Send(emailMsg)
           
            '5) do something - like let the user know it was sent
            span_output.InnerHtml = "Email sent to " & txt_emailTo.Text
            '...done!
        Catch Exp As Exception
            span_output.InnerHtml = "An Error occured. <br>Error: " & Exp.ToString
        End Try

    End Sub
</Script>
   <Body>
      <B>
         <Font Color="DarkGreen" Face=Helvetica Size=5> Sending email in ASP.Net - test script provided via 
         123vISP.co.uk

         </Font>
          <HR Size="2" Color=Black >
          <P>
          <Form id="Form1" Method="Post"  EncType="Multipart/Form-Data" RunAt="Server">
          Send email to: <BR>
         &nbsp;<asp:TextBox ID="txt_emailTo" runat="server"></asp:TextBox>
         <Input id="btn_send" Type="Submit" Value="Send" OnServerclick="btn_send_Click"
              RunAt="Server">
         <P>
         <Div ID="div_output" Visible="True" RunAt="Server">
            <Span ID="span_output" RunAt="Server"/></Div>
         </Form>
   </Body>
</html>





...or try this more advanced script:

<%@ Page Language="vb" %>
<html>
<Script Language="VB" RunAt="Server">
    'NOTES:
    '   *** Replace me@mycompany.com with your actual email address
    '   *** Replace mail.mycompany.com with your actual SMTP server
    '   *** Replace username and password with your actual SMTP server credentials to send using SMTP
   
    Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            'As this is the first time the page has loaded, see if variables are passed in querystring
            If Request.QueryString("from") <> Nothing Then
                txt_from.Text = Request.QueryString("from")
            End If
            If Request.QueryString("subject") <> Nothing Then
                txt_subject.Text = Request.QueryString("subject")
            End If
            If Request.QueryString("message") <> Nothing Then
                txt_message.Text = Request.QueryString("message")
            End If
            If Request.QueryString("to") <> Nothing Then
                txt_to.Text = Request.QueryString("to")
            End If
            If Request.QueryString("server") <> Nothing Then
                txt_server.Text = Request.QueryString("server")
            End If
            If Request.QueryString("username") <> Nothing Then
                txt_username.Text = Request.QueryString("username")
            End If
            If Request.QueryString("password") <> Nothing Then
                txt_password.Text = Request.QueryString("password")
            End If
        End If
    End Sub
    Sub btn_send_Click(ByVal Sender As Object, ByVal e As EventArgs)
        Try
            'Declare our variables here...
            Dim emailMsg As New System.Net.Mail.MailMessage
            Dim smtp As New System.Net.Mail.SmtpClient(txt_server.Text, 25) '25=port 25
           
            '1) set the email addresses
            emailMsg.From = New System.Net.Mail.MailAddress(txt_from.Text)
            emailMsg.To.Add(txt_to.Text)
           
            '2) set the email content
            emailMsg.IsBodyHtml = True 'let's make this email nice HTML format rather than plain text
            emailMsg.Subject = txt_subject.Text
            emailMsg.Body = txt_message.Text
           
            '3) set the SMTP server credentials
            smtp.Credentials = New Net.NetworkCredential(txt_username.Text, txt_password.Text)
           
            '4) send the email message
            smtp.Send(emailMsg)
           
            '5) do something - like let the user know it was sent
            output.InnerHtml = "<br><br><hr>Email sent to " & txt_to.Text & " at " & Now.ToString
            '...done!
        Catch Exp As Exception
            output.InnerHtml = "An Error occured. <br>Error: " & Exp.ToString
        End Try
    End Sub
    Sub btn_create_Click(ByVal Sender As Object, ByVal e As EventArgs)
        Dim txt As String = "sendmail.aspx?from=" & txt_from.Text & _
        "&subject=" & txt_subject.Text & "&message=" & txt_message.Text & _
        "&to=" & txt_to.Text & "&server=" & txt_server.Text & _
        "&username=" & txt_username.Text & "&password=" & txt_password.Text
       
        url_querystring.Text = txt
        url_querystring.NavigateUrl = txt
    End Sub
</Script>
   <Body>
      <B>
         <Font Color="DarkGreen" Face=Helvetica Size=5> Sending email in ASP.Net - test script provided via
         ispsupport.123vISP.co.uk
         </Font>
          <HR Size="2" Color=Black >
          <P>
          <Form id="Form1" Method="Post"  EncType="Multipart/Form-Data" RunAt="Server">
          Send email
      <B>
          from
          <asp:TextBox ID="txt_from" runat="server">from=</asp:TextBox>
          &nbsp;</b>with subject
      <B>
          <asp:TextBox ID="txt_subject" runat="server">subject=</asp:TextBox>
         &nbsp; and message...<br />
          <asp:TextBox ID="txt_message" runat="server" Columns="80" Rows="5"
              TextMode="MultiLine">message=</asp:TextBox>
          <br />
          ...</b>to:
         &nbsp;<asp:TextBox ID="txt_to" runat="server">to=</asp:TextBox>
         &nbsp;<br />
          ...using SMTP server
      <B>
          <asp:TextBox ID="txt_server" runat="server">server=</asp:TextBox>
          , username
          <asp:TextBox ID="txt_username" runat="server">username=</asp:TextBox>
          , password
          <asp:TextBox ID="txt_password" runat="server">password=</asp:TextBox>
          .<br />
          Complete the fields on this page, then click
         <Input id="btn_send" Type="Submit" Value="Send" OnServerclick="btn_send_Click"
              RunAt="Server"><br />
          <br />
          You can pass variables to this page using a querystring such as:<br />
          <asp:HyperLink ID="url_querystring" runat="server" Font-Names="Arial"
              Font-Size="10px"
              NavigateUrl="mailto:sendmail.aspx?from=me@mymail.com&subject=Hello&message=Just a test...&amp;to=mybuddy@remotemail.com&amp;server=mail.mymail.com&amp;username=me&amp;password=1234"
              Target="_blank">sendmail.aspx?from=me@mymail.com&amp;subject=Hello&amp;message=Just a test...&amp;to=mybuddy@remotemail.com&amp;server=mail.mymail.com&amp;username=me&amp;password=1234</asp:HyperLink>
          <br />
         <Input id="btn_create" Type="Submit" Value="Create URL using above form values" OnServerclick="btn_create_Click"
              RunAt="Server"></b><Div ID="div_output" Visible="True" RunAt="Server">
            <Span ID="output" RunAt="Server"/></Div>
         </Form>
   </Body>
</html

Article ID: 26, Created On: 10/19/2008, Modified: 3/29/2011

Comments (0)