Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<body bgcolor="#ccccff">
    <form id="form1" runat="server">
    <div>
    <table width="962" bgcolor="#ccccff">
    <tr>
    <td class="style5">
        <asp:Button ID="btnSend" Text="Send" runat="server" Font-Size="Small"
            onclick="btnSend_Click2"/></td>
        <td class="style6">
            <asp:Button ID="btnSave" Text="Save" runat="server"
                Font-Size="Small" onclick="btnSave_Click"/></td>
        <td>
            <asp:Label ID="Lbldisplay" runat="server" Text="Label"></asp:Label>
        </td>






    </tr>

    </table>
    <hr />

    </div>
    <table class="style1" width="962" align="center">
        <tr>
            <td class="style4">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="lblFrm" runat="server" Text="From :-" Font-Size="Small"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TxtFrm" runat="server" Height="22px" Width="263px"
                    Enabled="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="lblto" runat="server" Text="To :-" Font-Size="Small"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="Txtto" runat="server" Height="22px" Width="263px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="Lblcc" runat="server" Text="Cc :-" Font-Size="Small"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="Txtcc" runat="server" Height="22px" Width="263px"></asp:TextBox>
            </td>
        </tr>
        <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="Lblcc0" runat="server" Text="Bcc :-" Font-Size="Small"></asp:Label>
            &nbsp;</td>
            <td>
                <asp:TextBox ID="txtbcc" runat="server" Height="22px" Width="263px"></asp:TextBox>
            </td></tr>
        <tr><td class="style4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="lblattach0" Text="Subject :-" runat="server" Font-Size="Small"></asp:Label>&nbsp;</td>
            <td>
                <asp:TextBox ID="Txtsub" runat="server" Height="22px" Width="263px"></asp:TextBox>
            &nbsp; attachment:<asp:FileUpload ID="attachmenttext" runat="server" />
            </td>
            </tr>

    </table>
    <hr />
    <table class="style1">
        <tr>
            <td>
                <asp:TextBox ID="Txtbody" runat="server" Height="267px" Width="896px"></asp:TextBox>
            </td>
        </tr>
    </table>
    <p>
        <asp:Label ID="Lblsuccessfully" runat="server" Text="Label" Font-Size="Small"></asp:Label>
            </p>
    </form>
</body>


protected void btnSend_Click2(object sender, EventArgs e)
    {
        try
        {
           
                MailMessage ms = new MailMessage("test@gmail.com",Txtto.Text,Txtsub.Text,Txtbody.Text);
                ms.IsBodyHtml = false;
                NetworkCredential nc = new NetworkCredential("test@prayoglabs.com", "prayoglabs@123");
                SmtpClient obj = new SmtpClient("mail.gmail.com", portnumber);
           
                if(txtbcc.Text!="")
                ms.Bcc.Add(txtbcc.Text);
                if(Txtcc.Text!="")
                ms.CC.Add(Txtcc.Text);
                if (attachmenttext.HasFile)
                {
                    string atach = Path.GetFileName(attachmenttext.PostedFile.FileName);
                    Attachment myattach = new Attachment(attachmenttext.FileContent, atach);
                    ms.Attachments.Add(myattach);
                }
                obj.UseDefaultCredentials = false;
                obj.Credentials = nc;
                obj.Send(ms);
           
            Lblsuccessfully.Text = "Mail Send Successfully";
            Txtto.Text = " ";
            Txtcc.Text = " ";
            Txtsub.Text = " ";
            Txtbody.Text = " ";
            txtbcc.Text = " ";
        }
        catch (Exception ex)
        {
            Lblsuccessfully.Text = ex.Message;
        }
Posted
Updated 2-Jun-11 3:33am
v2
Comments
Karthik. A 2-Jun-11 9:34am    
So what's your question?
PS - Added pre tags
GenJerDan 2-Jun-11 9:38am    
No, see this is Questions *AND* Answers. This is an answer. Now it'll be here in case someone comes in and asks how to send email with an attachment from and ASP.net page. Efficient, yes?

1 solution

There is a generic routine here: Sending an Email in C# with or without attachments: generic routine.[^] - at the very least it handles attachments better...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900