Friday 23 November 2012

Send Email with Attachment in asp.net

Sending Email With Attachments:

<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>
    <asp:ScriptManager ID="ScriptManager1" runat="server">      
    </asp:ScriptManager>

<table >
            <tr>
        <td align="center">
     
            <table cellpadding="0" cellspacing="0" >            
            <tr>
                                <td >
                                        Name
                                </td>
                                <td >
                                <asp:TextBox ID ="txtName" runat ="server"></asp:TextBox>
                               
                                </td>
            </tr>
            <tr>
                                <td >
                                        From Mail
                                </td>
                                <td >
                                <asp:TextBox ID ="txtfrommail" runat ="server" Width="350px" Height="17"
                                        MaxLength="50" ></asp:TextBox>                              
                               
                                </td>
            </tr>
            <tr>
                                <td >
                                        To Mail
                                </td>
                                <td >
                                <asp:TextBox ID ="txttomail" runat ="server"></asp:TextBox>                                    
                                 
                                </td>
            </tr>
            <tr>
                                <td >
                                        Password
                                </td>
                                <td >
                                <asp:TextBox ID ="txtpassword" runat ="server" Width="350px" Height="17" MaxLength="20" TextMode="Password"></asp:TextBox>                                    
                                 
                                </td>
            </tr>          
            <tr>
                                <td >
                                        Subject
                                </td>
                                <td >
                                  <asp:TextBox ID ="txtsubject" runat ="server" Width="350px" Height="17"
                                        MaxLength="200" ></asp:TextBox>                                                                                                            
                                </td>
            </tr>
            <tr>
                                <td >
                                        Body
                                </td>
                                <td>                                                                                                                                          
                                     <HTMLEditor:Editor runat="server" Id="editor" Height="100px"
                                                                                        AutoFocus="true" Width="100%" />
                                   
                                </td>
            </tr>
             <tr>
                                <td>
                                        Attachments
                                </td>
                                <td >                                    
                                    <asp:FileUpload ID="FileUpload1" runat="server"/>                                                  
                             
            </tr>
            <tr>
                                <td >
                                <asp:Button ID="btnsend" runat="server" Text="click to send mail"                   onclick="btnsend_Click" OnClientClick="return valiedbody()"/>
           
                                <asp:Button ID="btncancel" runat="server" Text="Cancel" />                                                    
                                </td>
              </tr>
              <tr><td colspan="2"><asp:Label ID="lblmessage" runat="server"></asp:Label></td></tr>                                      
                </table>
               </td>
               </tr>
               </table>

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.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

public partial class SentMail : System.Web.UI.Page
{
    protected void Page_Load(object sender,EventArgs e)
    {
        if (!IsPostBack)
        {
         
        }
    }

    protected void btnsend_Click(object sender, EventArgs e)      
    {    

        string str = "";

        string pno = "";

        string frommail = txtfrommail.Text.ToString() ;

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

        if (mystr.ToString() == "gmail.com")
        {
            str = "smtp.gmail.com";

            pno = "587";
        }
        else if (mystr.ToString() == "yahoo.com")
        {
            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);

         if (FileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream,
                                                                                                 FileUpload1.FileName));
        }
         mail.Subject = txtsubject.Text;

        string body = editor.Content;

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

       mail.Body =strtext.ToString();

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

        lblmessage.Text = "Message successfully sent";

        txtName.Text = "";

        txtfrommail.Text = "";

        txttomail.Text = "";

        txtsubject.Text = "";    
    }
}


No comments:

Post a Comment