带附件的邮件发送 [英] mail sending with attachment

查看:81
本文介绍了带附件的邮件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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;
        }

推荐答案

此处有一个通用例程:
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...


这篇关于带附件的邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆