Sunday, 2 December 2012

Mail sending code with Cc, Bcc and multiple dynamic File Attachments

Mail sending code with Cc, Bcc and multiple dynamic File Attachments


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script type = "text/javascript">
    var counter = 0;
    function AddFileUpload() {
        var div = document.createElement('DIV');
        div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +
                     '" type="file" />' +
                     '<input id="Button' + counter + '" type="button" ' +
                     'value="Remove" onclick = "RemoveFileUpload(this)" />';
        document.getElementById("FileUploadContainer").appendChild(div);
        counter++;
    }
    function RemoveFileUpload(div) {
        document.getElementById("FileUploadContainer").removeChild(div.parentNode);
    }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
<br />
<table>
<tr>
<td style="width:25px">&nbsp;</td>
    <td>
        To</td>
    <td><asp:TextBox ID="txtto" runat="server" Width="250px" Height="22px"></asp:TextBox></td>
</tr>
<tr>
<td style="width:25px">&nbsp;</td>
    <td>
        CC</td>
    <td><asp:TextBox ID="txtcc" runat="server" Width="250px" Height="22px"></asp:TextBox></td>
</tr>
<tr>
<td style="width:25px">&nbsp;</td>
    <td>
        BCC</td>
    <td><asp:TextBox ID="txtbcc" runat="server" Width="250px" Height="22px"></asp:TextBox></td>
</tr>
<tr>
<td>&nbsp;</td>
    <td>
        Subject</td>
    <td><asp:TextBox ID="txtsubject" runat="server" Width="250px"
            ontextchanged="txtsubject_TextChanged"></asp:TextBox></td>
</tr>
<tr>
<td>&nbsp;</td>
    <td>
        &nbsp;</td>
        <td>
       
            <span style ="font-family:Arial">Click to add files</span>&nbsp;&nbsp;
    <input id="Button1" type="button" value="add" onclick = "AddFileUpload();" />
    <br /><br />
    <div id = "FileUploadContainer">
        <asp:FileUpload ID="fileUpload1" runat="server" />
    </div>
    <br />
        </td>

</tr>
<tr>
<td>&nbsp;</td>
    <td>
        Message</td>
    <td><asp:TextBox ID="txtmessage" runat="server" Width="350px" TextMode="MultiLine" Height="60px"></asp:TextBox></td>
</tr>
<tr>
<td>&nbsp;</td>
    <td colspan="2">
        <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>
    </td>
</tr>
<tr>
<td>
    &nbsp;</td>
    <td colspan="2">
        <center>
            <asp:Button ID="bensave" runat="server" onclick="bensave_Click" Text="Send" />
        </center>
    </td>
</tr>
</table>
</div>
    </div>
    </form>
</body>
</html>

Web.Config Configurations

<httpRuntime
  executionTimeout="110"
  maxRequestLength="4096"
  requestLengthDiskThreshold="80"
  useFullyQualifiedRedirectUrl="false"
  minFreeThreads="8"
  minLocalRequestFreeThreads="4"
  appRequestQueueLimit="5000"
  enableKernelOutputCache="true"
  enableVersionHeader="true"
  requireRootedSaveAsPath="true"
  enable="true"
  shutdownTimeout="90"
  delayNotificationTimeout="5"
  waitChangeNotification="0"
  maxWaitChangeNotification="0"
  enableHeaderChecking="true"
  sendCacheControlHeader="true"
  apartmentThreading="false"
/>

.cs Code

 protected void bensave_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtto.Text);
        if(txtcc.Text.Trim().Length!=0)
        {
            mail.CC.Add(txtcc.Text);
        }
        if (txtbcc.Text.Trim().Length != 0)
        {
            mail.Bcc.Add(txtbcc.Text);
        }
        mail.From = new MailAddress("salikrammaurya@gmail.com");
        mail.Subject = txtsubject.Text;
        mail.Body = txtmessage.Text;
        mail.IsBodyHtml = true;

        if (fileUpload1.HasFile)
        {
         

            for (int i = 0; i < Request.Files.Count; i++)
            {
                mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
             
            }

        }
 
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential("salikrammaurya@gmail.com", "password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
        lblmsg.Text = "Mail send successfully";
        clear();

    }
    public void clear()
    {
        txtto.Text = txtsubject.Text = txtmessage.Text = txtcc.Text = txtbcc.Text = string.Empty;
    }

No comments:

Post a Comment