Apr 16, 2011

Asp. net 2.0/3.5/4.0 Email code for gmail/hotmail/yahoo-1

default.aspx page

<%--<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <br />
        Subject :&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <br />
        Email :
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <br />
        Msg : &nbsp;&nbsp;
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" /></div>
    </form>
</body>--%>

default.aspx.cs page
public partial class _Default : System.Web.UI.Page
{
    EmailClass email = new EmailClass();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string file1 = FileUpload1.PostedFile.FileName;
        email.SendMail(TextBox3.Text, TextBox2.Text, TextBox1.Text);
        if (email.a)
        {
            Label1.Text = "Mail Sent !!";
        }
        else
        {
            Label1.Text = "Not Sent";
        }
    }
}

put this class file in app_code folder and save with name :  Email.cs

hello
public class EmailClass
{
    public bool a;
    public EmailClass()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void SendMail(string msg, string to, string subject)
    {
        //try
        //{
        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress("abc@gmail.com");
        mail.Subject = subject;

        string Body = null;
        Body = msg;
        mail.Body = Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        //smtp.UseDefaultCredentials = false;
        smtp.Host = "smtp.gmail.com";
           smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "password");
        smtp.EnableSsl = true;

        smtp.Send(mail);
        a = true;
        //}
        //catch (Exception ex)
        //{
        //ex.ToString();
        //a = false;
        // }


    }


}






No comments:

Image Gallery - Image Add/Delete/Preview - Single Page App -

https://github.com/TheKalin/jQuery-File-Upload.MVC5 Helper Class / Models   public class FilesHelperDB     {         dbImageGallery...