Copyrights © 2012 Jatin Kotadiya. All Rights Reserved . Powered by Blogger.

Saturday, November 3, 2012

VB .Net Email Example



Send email with attached file
<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMessage as String
    Dim TheMailMessage as New MailMessage
    Dim TheAttachment as MailAttachment
    TheMessage = txtName.Text _
        & ": Attached to this email is the " _
        & "file you requested."
    TheMailMessage.From = "us@a.com"
    TheMailMessage.To = txtEmailAddress.Text
    TheMailMessage.Subject = "File Request"
    TheMailMessage.Body = TheMessage
    TheAttachment = New MailAttachment( _
        Server.MapPath(ddlFile.SelectedItem.Value))
    TheMailMessage.Attachments.Add(TheAttachment)
    SmtpMail.SmtpServer = "localhost"
    SmtpMail.Send(TheMailMessage)

   

    lblMessage1.Text = "The file requested has been sent " _
        & "to the email address you entered.<BR>Enter Your Name"
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>File Request Sample Site</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label
    id="lblMessage1"
    runat="Server"
    Text="Enter Your Name"
/>
<BR>
<asp:TextBox
    id="txtName"
    runat="server"
    MaxLength=50
/>
<BR><BR>
<asp:Label
    id="lblMessage2"
    runat="Server"
    Text="And Your Email Address"
/>
<BR>
<asp:TextBox
    id="txtEmailAddress"
    runat="server"
    MaxLength=50
/>
<BR><BR>
<asp:Label
    id="lblMessage3"
    runat="Server"
    Text="Select the file you wish to download"
/>
<BR>
<asp:DropDownList
    id="ddlFile"
    runat="server"
> 
    <asp:ListItem
        Value="Catalog.txt"
        Text="Catalog"
    />
    <asp:ListItem
        Value="StoreLocations.txt"
        Text="Store Locations"
    />
</asp:DropDownList>
<BR><BR>
<asp:button
    id="butOK"
    text="Send File"
    Type="Submit"
    OnClick="SubmitBtn_Click"
    runat="server"
/>
<BR>
</Font>
</Form>
</BODY>
</HTML>

Send email to all employees in database
<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    Dim DBConn as OleDbConnection
    Dim DBCommand As OleDbDataAdapter
    Dim DSPageData as New DataSet
    Dim I as Long
    TheMailMessage.From = txtFromEmail.Text
    TheMailMessage.Subject = txtSubject.Text
    TheMailMessage.Body = txtMessage.Text
    DBConn = New OleDbConnection( _
        "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        & "DATA SOURCE=" _
        & Server.MapPath("EmployeeDatabase.mdb;"))
    DBCommand = New OleDbDataAdapter _
        ("Select FirstName " _
        & "From Employee", DBConn)
    For I = 0 to DSPageData.Tables("FirstName"). _
        Rows.Count - 1
        TheMailMessage.To = DSPageData.Tables("FirstName"). _
            Rows(I).Item("FirstName") & "@hotmail.com"
        TheMailConnection.Send(TheMailMessage)
    Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sending Email Blast</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<BR>
<asp:textbox
    id="txtFromEmail"
    runat="server"
/>
<BR>
Enter the subject of your message:
<BR>
<asp:textbox
    id="txtSubject"
    runat="server"
/>
<BR>
Enter the text of your message:
<BR>
<asp:textbox
    id="txtMessage"
    runat="server"
    textmode="MultiLine"
    rows="5"
/>
<BR>
<asp:button
    id="butOK"
    text="Send"
    Type="Submit"
    OnClick="SubmitBtn_Click"
    runat="server"
/>
</form>
</BODY>
</HTML>

Phone Book Demo (VB.net)

<%--
Code revised from

ASP.NET Tips & Techniques (Paperback)
by Greg Buczek


# Publisher: McGraw-Hill/Osborne Media; 1st edition (May 21, 2002)
# Language: English
# ISBN: 0072225149

--%>  
<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim DBConn as OleDbConnection
        Dim DBCommand As OleDbDataAdapter
        Dim DSPageData as New DataSet
        DBConn = New OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath _
            ("PhoneDir.mdb;"))
        DBCommand = New OleDbDataAdapter _
            ("Select EmpName as [Employee Name], " _
            & "Department, PhoneNumber as " _
            & "[Phone Number] From Emps " _
            & "Order By EmpName", DBConn)
        DBCommand.Fill(DSPageData, _
            "Employees")
        dgEmps.DataSource = _
            DSPageData.Tables("Employees").DefaultView
        dgEmps.DataBind()
        DBCommand = New OleDbDataAdapter _
            ("Select Distinct Department From Emps " _
            & "Order By Department", DBConn)
        DBCommand.Fill(DSPageData, _
            "Departments")
        ddlDepartments.DataSource = _
            DSPageData.Tables("Departments").DefaultView
        ddlDepartments.DataBind()
    End If
End Sub
Sub ddl1_Changed(Sender As Object, E As EventArgs)
    Dim DBConn as OleDbConnection
    Dim DBCommand As OleDbDataAdapter
    Dim DSPageData as New DataSet
    DBConn = New OleDbConnection( _
        "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        & "DATA SOURCE=" _
        & Server.MapPath _
        ("PhoneDir.mdb;"))
    DBCommand = New OleDbDataAdapter _
        ("Select EmpName as [Employee Name], " _
        & "Department, PhoneNumber as " _
        & "[Phone Number] From Emps " _
        & "Where Department = '" _
        & ddlDepartments.SelectedItem.Text _
        & "' Order By EmpName", DBConn)
    DBCommand.Fill(DSPageData, _
        "Employees")
    dgEmps.DataSource = _
        DSPageData.Tables("Employees").DefaultView
    dgEmps.DataBind()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Phone Directory</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:label
    id="lblMessage1"
    font-size="12pt"
    font-bold="True"
    font-name="Lucida Console"
    text="Phone Number List"
    runat="server"
/>
<BR><BR>
<asp:datagrid
    id="dgEmps"
    runat="server"
    autogeneratecolumns="True"
/>
<BR><BR>
<asp:label
    id="lblMessage2"
    font-size="10pt"
    font-name="Lucida Console"
    text="Filter by Department"
    runat="server"
/>
<BR>
<asp:dropdownlist
    id="ddlDepartments"
    datatextfield="Department"
    autopostback="True"
    onselectedindexchanged="ddl1_Changed"
    runat="server"
/>
</form>
</BODY>
</HTML>

2 comments:

  1. Really Good Work Done By You...However, stopping by with great quality writing, it's hard to see any good blog today.
    Freeprosoft
    Recover My Files Crack
    Crack Softwares Free Download

    ReplyDelete

  2. This website's content is more useful. And thanks for sharing.
    https://crackmypc.com/dgflick-album-xpress-pro/

    ReplyDelete