Step 1:
Create a page named as SendMail.aspx and write the below code :
<div>
<asp:Label ID=”Label1″ runat=”server”>Name: </asp:Label>
<asp:TextBox ID=”txtName” runat=”server”></asp:TextBox>
</div>
<div>
<asp:Label ID=”Label2″ runat=”server”>Mobile: </asp:Label>
<asp:TextBox ID=”txtTelephone” runat=”server”></asp:TextBox>
</div>
<div>
<asp:Label ID=”Label2″ runat=”server”>Email: </asp:Label>
<asp:TextBox ID=”txtEmail” runat=”server”></asp:TextBox>
</div>
<div>
<asp:Button ID=”btnSend” runat=”server” onclick=”btnSend2_Click” Text=”Send Mail” />
</div>
Step 2:
In the SendMail.asp.cs page write the below code, its Done.
protected void SendMail()
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(“smtp.gmail.com”);
mail.From = new MailAddress(“bighna@gmail.com”);
mail.To.Add(“bighnaraj@outlook.com”);
mail.Subject = “Email Sending Test”;
mail.IsBodyHtml = true;
string mailBody = “Name : ‘” + txtName.Text.ToString() + “‘ <br /><br />Email ID : ” + txtEmail.Text.ToString() + ” <br /><br />Mobile No : ” + txtTelephone.Text.ToString() + “”;
mail.Body = mailBody;
SmtpServer.Credentials = new System.Net.NetworkCredential(” bighna@gmail.com”, “your a/c password”);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
}
If u want to use another way to send mail with attachment accoring to Network credential also follow the below code.
public static bool sendEmail(string hostName, int port, string fromEmail, string[] toEmail, string[] toEmailCC, string[] toEmailBCC, string subject, string body, string replyToEmail, bool IsHtml, string[] attachmentPath, bool IsAuthenticationRequired, string username, string password, string domain)
{
int i = 0;
bool status = false;
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(fromEmail);
for (i = 0; i < toEmail.Length; i++)
{
message.To.Add(new MailAddress(toEmail[i]));
}
for (i = 0; i < toEmailCC.Length; i++)
{
message.CC.Add(new MailAddress(toEmailCC[i]));
}
for (i = 0; i < toEmailBCC.Length; i++)
{
message.Bcc.Add(new MailAddress(toEmailBCC[i]));
}
message.Subject = subject;
if (IsHtml == true)
{
message.IsBodyHtml = true;
}
message.Body = body;
if (replyToEmail.Trim().Length != 0)
{
message.Headers.Add(“Reply-To”, replyToEmail);
}
else
{
message.Headers.Add(“Reply-To”, fromEmail);
}
for (i = 0; i < attachmentPath.Length; i++)
{
string attachPath = attachmentPath[i].Replace(“\\”, “\\\\”);
message.Attachments.Add(new Attachment(attachPath)); ;
}
SmtpClient client = new SmtpClient(hostName, port);
if (IsAuthenticationRequired == true)
{
if (domain.Trim().Length == 0)
{
client.Credentials = new System.Net.NetworkCredential(username, password);
}
else
{
client.Credentials = new System.Net.NetworkCredential(username, password, domain);
}
}
client.Send(message);
status = true;
message.Dispose();
}
catch (Exception)
{
status = false;
}
return status;
}
On click of an button event use the below code
protected void btnSend_Click(object sender, EventArgs e)
{
string mail_text = “”;
mail_text = mail_text + “<b>Name : </b>txtName.Text<br>”;
mail_text = mail_text + “<b>Email id : </b>txtEmail.Text<br>”;
try
{
string strSubject = “1: Feedback”;
string[] toEmail = { “bighnaraj@gmail.com” };
string[] toEmailcc = { “bighnaraj@gmail.com };
string[] toEmailBcc = { };
string[] toAttachment = { };
string username = “do@username.com”;
string password = “password”;
string fromEmail = “do@username.com”;
string mailServer = “Mail.server.com”;
string domain = “”;
string rply = “”;
bool status = false;
status = sendEmail(mailServer, 25, fromEmail, toEmail, toEmailcc, toEmailBcc, strSubject, mail_text, rply, true, toAttachment, true, username, password, domain);
}
catch (Exception ex)
{
throw ex;
}
}