Tuesday 17 July 2012

Send Email through asp.net.


Email.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                   
                </td>
                <td>
                    <asp:TextBox ID="NameTextBox" runat="server" Visible="False">Admin Name </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:TextBox ID="FromEmailTextBox" runat="server" Visible="False">adminemailaddress@gmail.com</asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Label ID="ToEmailLabel" runat="server" Text="To Email"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="ToEmailTextBox" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    <asp:Button ID="Button1" runat="server" Text="Button" Width="102px"
                        onclick="Button1_Click" />
                   </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    <asp:Label ID="SendLabel" runat="server" Text="Email Send" Visible="False"></asp:Label>
                   </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    <asp:Label ID="ErrorLabel" runat="server" Text="Error Occured" Visible="False"></asp:Label>
                   </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                   </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



Email.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
    {
               

        String[] ToID = (ToEmailTextBox.Text).Split(',');  // for seprate multiple email addresses

        try
        {
            MailMessage MyMailMessage = new MailMessage();
            for (int x = 0; x <= ToID.Length - 1; x++)
            {
            MyMailMessage.To.Add(ToID[x].ToString());
            }
            MyMailMessage.From = new MailAddress(FromEmailTextBox.Text);
            MyMailMessage.To.Add(ToEmailTextBox.Text);
            MyMailMessage.Subject = "Hi" +NameTextBox.Text;

            MyMailMessage.Body = "This is a Test Mail.......";
           
 if(FileUpload1.HasFile){
MyMailMessage.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
 }

            SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
            SMTPServer.Port = 25;
            SMTPServer.Credentials = new System.Net.NetworkCredential("ur email address", "ur email password");
            SMTPServer.EnableSsl = false;
            SMTPServer.Send(MyMailMessage);
           
       
                    SendLabel.Visible = true;
        }
        catch (Exception ex)
        {
            ErrorLabel.Visible = true;
        }
    }

No comments:

Post a Comment