DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Email User Control VB .NET

05.02.2007
| 8985 views |
  • submit to reddit
        Save as an .ascx file and insert into your project. 
Set properties via the properties window. 
Includes the form, code, validation, and css.

<%@ Control Language="VB" ClassName="Email" %>
<%@ Import Namespace="System.Net.Mail" %>



<script runat="server">
    Public Property Email() As String
        Get
            Return recipientEmail
        End Get
        Set(ByVal value As String)
            recipientEmail = value
        End Set
    End Property


    Public Property Host() As String
        Get
            Return mhost
        End Get
        Set(ByVal value As String)
            mhost = value
        End Set
    End Property


    Public Property Port() As String
        Get
            Return mport
        End Get
        Set(ByVal value As String)
            mport = value
        End Set
    End Property


    Public Property Message() As String
        Get
            Return sentMessage
        End Get
        Set(ByVal value As String)
            sentMessage = value
        End Set
    End Property
    
    Dim recipientEmail As String
    Dim mhost As String
    Dim mport As Integer
    Dim sentMessage As String
    Dim client As New Net.Mail.SmtpClient()

    Protected Sub btnSendMail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendMail.Click

        client.Host = Host
        client.Port = Port
        client.Send(txtSenderEmail.Text, recipientEmail, txtSubject.Text, txtMessage.Text)
        lblMessage.Text = sentMessage
    End Sub
</script>

<style type="text/css">
  label
  {
   	   float: left;
   	   width:10em;
   	   text-align:right;
   	   clear:left;
   	   margin-right: 7px;
   	   font-family: Tahoma, Sans-Serif;
   	   font-size:12px;
   	   font-weight:bold;
  	    padding:4px;
   		background:#FFFFFF;
   		color:#333333;
  }
  
  .validate
  {
    font-family: Tahoma, Sans-Serif;
   	font-size:12px;
  }
  
  </style>
  
<label>Email:</label><asp:TextBox ID="txtSenderEmail" runat="server" Width="375px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSenderEmail"
    ErrorMessage="Required!" CssClass="validate"></asp:RequiredFieldValidator><br />

<label>Subject:</label><asp:TextBox ID="txtSubject" runat="server" Width="375px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtSubject"
    ErrorMessage="Required!" CssClass="validate"></asp:RequiredFieldValidator><br />
<label>Message:</label><asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"
        Height="160px" Width="375px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtMessage"
    ErrorMessage="Required!" CssClass="validate"></asp:RequiredFieldValidator><br />
<label><asp:Label ID="lblMessage" runat="server"></asp:Label></label><asp:Button ID="btnSendMail"
        runat="server" Text="Send" />