Friday, September 23

Simple code to Send a mail by using Smtp.Host as gmail.com


Coding Part:-
use namespace:
using System.Net.Mail;

protected void btnSendmail_Click(object sender, EventArgs e)
    {
try
{

MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
mail.From = new MailAddress(txtEmail.Text);
// mail.To.Add(new MailAddress(txtTo.Text));
mail.To.Add(new MailAddress("shibashishXYZ@gmail.com"));

mail.Subject = txtSubject.Text;
mail.Body =txtName.Text + txtMessage.Text;
mail.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";


//smtp.Host = "localhost";

smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "txtEmail.Text";
NetworkCred.Password = "1234";
//NetworkCred.UserName = "xyz@shibashish.com";
// NetworkCred.Password = "Password123!";
smtp.UseDefaultCredentials = true;
//smtp.Credentials = NetworkCred;
smtp.Credentials = new System.Net.NetworkCredential("txtEmail.Text", "1234");

smtp.Port = 587;//this is Gmail port for e-mail
//smtp.Port = 25;// this is default port no
smtp.Send(mail);//send an e-mail

lblStatus.Text="Message Sended";
}
catch(Exception ex)
{
throw ex;
}
}


Design View:-

<div class="col_w420 float_l">
          
             <h3>Contact Form</h3>
              
              <div id="cp_contact_form">
              <body>
              <form runat="server" id="frmSend">
              
                    <div>
        <h2 style="background-color:Brown; color:Wheat; font-family:Verdana; font-size:14px" align=center>Please enter the following requested 
information below to send us your Message.</h2>
<table align=center>
<tr>
<td class="style1">
                        <label for="author">
                       
Name:</label></td>
<td><asp:textbox id="txtName" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td class="style1">
                        <label for="email">
                        Email:</label></td>
<td><asp:textbox id="txtEmail" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td class="style1">
                        <label for="subject">
                        Subject:</label></td>
<td><asp:textbox id="txtSubject" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td colSpan="2" >
                        <label for="text">
                        Message:</label></td>
</tr>
<tr>
<td align="center" colSpan="2" width=100%><asp:textbox id="txtMessage" Width="99%" 
                            Runat="server" Height="99px" TextMode="MultiLine" MaxLength="400"></asp:textbox></td>
</tr>
<tr>
<td colSpan="2">&nbsp;</td>
</tr>
<tr>
<td align=center class="style1"><asp:button id="btnSendmail" Runat="server" Text="Send " 
                            OnClick="btnSendmail_Click"></asp:button></td>
<td align=center><asp:button id="btnReset" Runat="server" Text="Reset" OnClick="btnReset_Click"></asp:button></td>
</tr>
<tr>
<td colSpan="2"><asp:label id="lblStatus" Runat="server" EnableViewState="False"></asp:label></td>
</tr>
</table>
    </div>
    </form>
    </body>
            </div>
               
      </div>

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

ShibashishMnty
shibashish mohanty