Friday 4 January 2013

sending email with multiple images using asp.net


sending email With multiple attachments:

<html>
<head runat="server">
    <title></title>

<script type="text/javascript">
 
       function valiedbody() {
   
           if (document.getElementById("<%=editor.ClientID%>").value == "") {                                     
                 
               if (confirm('Do you want to send with out body')) {
                   return true;
                }
               else {
                   document.getElementById("<%=editor.ClientID%>").focus();
                   return false;
               }          
               return false;
           }
           if (document.getElementById("<%=txtsubject.ClientID%>").value == "") {

               if (confirm('Do you want to send without subject')) {
                   return true;
               }
               else {
                   document.getElementById("<%=txtsubject.ClientID%>").focus();
                   return false;
               }
               return false;
           }    
           }       
</script>

</head>
<body>
    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
     <table >
            <tr>
        <td align="center">    
            <table cellpadding="0" cellspacing="0">      
            <tr>                           
                                <td >
                                <asp:TextBox ID ="txtName" runat ="server"></asp:TextBox>                             
                                </td>
            </tr>
            <tr>
                         
                                <td >
                                <asp:TextBox ID ="txtfrommail" runat ="server" ></asp:TextBox>                                                          
                                </td>
            </tr>
            <tr>                         
                                <td >
                                <asp:TextBox ID ="txttomail" runat ="server"></asp:TextBox>                      
                                 </td>
            </tr>             
            <tr>                         
                                <td >
                           
                                <asp:TextBox ID="txtpassword" runat="server" Enabled="false"  Width="350"
                                        TextMode="Password"></asp:TextBox>                          
                                </td>
            </tr>      
            <tr>                       
                                <td >
                                  <asp:TextBox ID ="txtsubject" runat ="server" Width="350px" Height="17"
                                        MaxLength="200" ></asp:TextBox>                                                                                                                                                
                                </td>
            </tr>
            <tr>                         
                                <td class="Col_Right1">                                                                                                                                        
                                     <HTMLEditor:Editor runat="server" Id="editor" Height="100px"
                                                                                           AutoFocus="true" Width="100%" />                                                                                                
                                </td>
            </tr>     
             <tr>
             <td >                        
              <asp:UpdatePanel ID="update1" runat="server" updatemode="Conditional">                                                                                              
             <ContentTemplate>          
                 Attachment                                                    
              <asp:FileUpload ID="FileUpload1" runat="server"/>                                                                                                                    
                                    <asp:Button ID="btnfileupload" runat="server" Text="Upload"
                                        onclick="btnfileupload_Click"/>                                  
                                        <br />
                                        <asp:Label ID="lblattchament1" runat="server" ></asp:Label><br />                                      
                                                                                                                                               
             </ContentTemplate>        
                                <Triggers><asp:PostBackTrigger ControlID="btnfileupload" /></Triggers>                                                                                        
            </asp:UpdatePanel>
            </td>
           </tr>   
            <tr>                     
                                <asp:Button ID="btnsend" runat="server" Text="click to send mail"
                                        OnClientClick="return valiedbody()" onclick="btnsend_Click"/>                                                                              
                                </td>                          
              </tr>
              <tr><td colspan="2"><asp:Label ID="lblmessage" runat="server"></asp:Label></td></tr>                                    
                </table>
               </td>
               </tr>
               </table>

</form>
</body>
</html>


using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web;
using System.Net.NetworkInformation;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

public partial class Newsendmail : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
   
            if (!IsPostBack)
            {
                ViewState["Array"] = null;
           
                  //Delete Items with in folder          

                foreach (var f in System.IO.Directory.GetFiles(Server.MapPath("~/NewFolder1")))

                    System.IO.File.Delete(f);
            }
   
    }
    protected void btnsend_Click(object sender, EventArgs e)
    {
        string str = "";

        string pno = "";
        try
        {
            string frommail = txtfrommail.Text.ToString();

            string mystr = frommail.Substring(frommail.Length - 9);       

            if (mystr.ToString() == "gmail.com") //this is for gmail
            {
                str = "smtp.gmail.com";

                pno = "587";
            }
            else if (mystr.ToString() == "yahoo.com")// this is for yahoo mail
            {
                str = "smtp.mail.yahoo.co.in";

                pno = "587";
            }
       
            SmtpClient smtp = new SmtpClient();

            MailMessage mail = new MailMessage();

            MailAddress madd = new MailAddress(txtfrommail.Text);
                   
            mail.From = madd;

            mail.To.Add(txttomail.Text);

            mail.Subject = txtsubject.Text;

            mail.IsBodyHtml = true;

            mail.Body = editor.Content;

           string body1 = editor.Content;

           string strtext = Regex.Replace(body1, @"<(.|\n)*?>", String.Empty);    

            ArrayList arrValues = new ArrayList();

            string projName = lblattchament1.Text;

           List<string> languages = projName.Split(new char[] { ',' }).ToList();      

            string Body = body1;

          if (ViewState["Array"] != null)
            {            
                ArrayList myarrli = (ArrayList)ViewState["Array"];
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body,
                                                                                                                                   null, "text/html");
               for(int i = 0;i<myarrli.Count;i++)
               {
                  LinkedResource imagelink = new LinkedResource(Server.MapPath("~/NewFolder1/"
                                                                                                                    + myarrli[i]), "image/jpeg");
                       imagelink.ContentId = "imageId"+(i+1);
                       imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                       htmlView.LinkedResources.Add(imagelink);
                   }
                  mail.AlternateViews.Add(htmlView);                
            }     
            smtp.Host = str;

            smtp.Port = Convert.ToInt32(pno);

            smtp.EnableSsl = true;

            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;      
               
              smtp.Credentials = new NetworkCredential(txtfrommail.Text, txtpassword.Text);
       
            smtp.Send(mail);

            mail.Dispose();

            lblmessage.Text = "Message successfully sent";

            ViewState["Array"] = null;
        }
        catch
        {
            lblmessage.Text = "Message sending fail";      
        }
        finally
        {
            txtName.Text = "";

            txtfrommail.Text = "";

            txttomail.Text = "";

            txtpassword.Enabled = false;

            lblattchament1.Text = "";

            txtsubject.Text = "";                  
        }
    }

    protected void btnfileupload_Click(object sender, EventArgs e)
    {
   
       if (FileUpload1.HasFile)
        {
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

            string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);

            string fileLocation = Server.MapPath("~/NewFolder1/" + fileName);

            FileUpload1.SaveAs(fileLocation);

            lblattchament1.Text += String.Format("{0},", FileUpload1.FileName);

            string projName = lblattchament1.Text;

            List<string> languages = projName.Split(new char[] { ',' }).ToList();

            editor.Content += Server.MapPath("~/NewFolder1/" + fileName) +
                        "<img alt=\"\" hspace=0 src=\"cid:imageId" + (languages.Count -1)+ "\"
                                                                                                 align=baseline border=0 >";
         
            if (lblattchament1.Text != "")
            {
                ArrayList myarrli = new ArrayList();

                if (ViewState["Array"] != null)
                {
                    myarrli = (ArrayList)ViewState["Array"];
                }
                myarrli.Add(FileUpload1.FileName);

                ViewState["Array"] = myarrli;
            }
         
        }  
    }
}

No comments:

Post a Comment