Sunday, 25 March 2012

Send Mail From ASP.net and C#

using System.Net.Mail;

 protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        try
        {
         
           MailMessage mailObj = new MailMessage(
                        txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
            SmtpClient SMTPServer = new SmtpClient(ser);
            SMTPServer.Credentials = new System.Net.NetworkCredential(usnam, pass);
            SMTPServer.Send(mailObj);
            Label1.Text = "Mail Sent successfully";
        }
        catch (Exception)
        {
            Label1.Text = "Check Your InternetConnection or email Configuration";
        }
    }
}
ser -> Smtp server Name.

You Can Also Try This Code,Using Attachment in Mail


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;
using System.Net;

public partial class sendmail : System.Web.UI.Page
{
    MySqlConnection con = new MySqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
    MySqlCommand cmd = new MySqlCommand();
    MySqlDataReader rd;
    string ser = "", usnam = "", pass = "", Client = "";
    MailMessage newmsg;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack == true)
        {


        }
        con.Open();
        cmd = new MySqlCommand("select Server,Username,Password,fromadd,toadd,Client_id from email_confi", con);
        rd = cmd.ExecuteReader();
        while (rd.Read())
        {
            ser = rd[0].ToString();
            usnam = rd[1].ToString();
            pass = rd[2].ToString();
            txtFrom.Text = rd[3].ToString();
            txtTo.Text = rd[4].ToString();
            Client = rd[5].ToString();
        }
        rd.Close();
        con.Close();


    }
    protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        try
        {
            MailAddress mailfrom = new MailAddress(txtFrom.Text);
            MailAddress mailto = new MailAddress(txtTo.Text);
            newmsg = new MailMessage(mailfrom, mailto);

              newmsg.Body = txtSubject.Text;
               string path = Server.MapPath("~/Excel/");
               FileUpload1.SaveAs(path + FileUpload1.FileName);
            Attachment att = new Attachment(path + FileUpload1.FileName);
       
            newmsg.Attachments.Add(att);
            SmtpClient smtp = new SmtpClient(ser, 587);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(usnam, pass);
            smtp.EnableSsl = true;
            smtp.Send(newmsg);
            lblStatus.Text = "Sent email (" + txtSubject.Text + ") to " + txtTo.Text;

        }
        catch (Exception EX)
        {
            lblStatus.Text = "Check Your InternetConnection or email Configuration" + EX;
        }
    }
}
 



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sendmail.aspx.cs" Inherits="sendmail" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajax" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="Stylesheet" href="css/tabcss.css" />
    <title></title>
</head>
<body bgcolor="#ffff99">
    <form id="form1" runat="server">
      <ajax:ToolkitScriptManager ID="sc" runat="server" ></ajax:ToolkitScriptManager>    
  <center>
<ajax:RoundedCornersExtender runat="server" ID="RCE" TargetControlID="Panel2" 
             Radius="15" Enabled="True" ></ajax:RoundedCornersExtender>
             <asp:Panel runat="server" ID="Panel2"  BackColor="Brown"
            Width="750px" >
            <asp:Label ID="Label6" runat="server" Text="Send Mail" ForeColor="White" 
                     Font-Bold="True" Font-Size="Larger" ></asp:Label>
            </asp:Panel>
            </center>         
   <%-- <div style="font-family: Verdana; font-size: small; font-weight: bold; font-style: normal; font-variant: normal; text-transform: none; color: #000000;  text-align: left; text-indent: inherit; white-space: normal; word-spacing: normal; letter-spacing: normal; width: 910px;background-color: #FFFFFF">
        <img src="images/send_mail.PNG" width="100%" height="30px" />
        </div>--%>
        <br />
        <div>
        <center>
   <ajax:RoundedCornersExtender runat="server" ID="RoundedCornersExtender1" TargetControlID="Panel3" 
             Radius="15" Enabled="True" ></ajax:RoundedCornersExtender>
             <asp:Panel runat="server" ID="Panel3"  BackColor="Brown"
            Width="750px" >
          
          <center>
    <table>
    <tr>
    <td  align="left">
       <asp:Label ID="Label2" runat="server" Text="MessageTo" CssClass="label"></asp:Label>
    </td>
    <td  align="left">
    <asp:textbox id="txtTo" runat="server" ReadOnly="True" Width="216px" 
            BackColor="#FFFFCC"></asp:textbox>
    </td>
    </tr>
     <tr>
    <td  align="left">
       <asp:Label ID="Label3" runat="server" Text=" Message from" CssClass="label"></asp:Label>
    </td>
    <td  align="left">
    <asp:TextBox runat="server" ID="txtFrom" ReadOnly="True" 
            Width="215px" BackColor="#FFFFCC"></asp:TextBox>
    </td>
    </tr>
     <tr>
    <td  align="left">
       <asp:Label ID="Label4" runat="server" Text="Subject" CssClass="label"></asp:Label>
    </td>
    <td  align="left">
     <asp:TextBox runat="server" ID="txtSubject" Width="366px"></asp:TextBox>
    </td>
    </tr>
     <tr>
    <td  align="left">
        <asp:FileUpload ID="FileUpload1" runat="server" /> 
      </td>
    <td align="left">
        <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
        </td>
    </tr>
    </table>
    <asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click"
            Text="Send Email" />
        <br />
        <br />
        <asp:Label ID="lblStatus" runat="server" CssClass="label" ForeColor="#ccffcc"></asp:Label>
        </center> 
            </asp:Panel>
   </center>   
   <br />       
    </div>
    
    </form>
</html>
</body>

No comments:

Post a Comment