Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
how can email can be send from asp.net web form,
what are the requirements for sending the Emails form Web App.

Regards,
aamir
Posted

 
Share this answer
 
Hi.....
use this code....
C#
private void Mail_sen()
    {
        try
        {
            string bBody = mailmsg();

            string email = TextBox4.Text;
            string sSub = "Contact_Us";
            SendMailmsg(bBody, email, email, email, sSub, "sendcc");
            //lblerror.Text = "Mail Sent Successfully.";
            //lblerror.ForeColor = Color.Green;
        }
        catch (Exception ex)
        {
            //lblerror.Text = "Send Email Failed.";
            //lblerror.ForeColor = Color.Red;
        }
    }

 private string mailmsg()
    {
        StringBuilder strHTML = new StringBuilder();
        strHTML.Append("<table width="70%">");
        strHTML.Append("<tr><td width="100%" colspan="2"><b>Dear  abc </b></td></tr>");

        strHTML.Append("<tr bgcolor="cayn"><center><table><tbody><tr><td width="100%" colspan="2">CONTACT </td></tr></tbody></table></center></tr>");
        strHTML.Append("<tr><td width="20%"><b>Name : </b></td><td width="80%">abc</td></tr>");
        strHTML.Append("<tr><td width="20%"><b>Email ID : </b></td><td width="80%">abc@gmail.com</td></tr>");
        strHTML.Append("<tr><td width="20%"><b>Phone No : </b></td><td width="80%">999999999</td></tr>");
        strHTML.Append("<tr><td width="20%"><b>Comments : </b></td><td width="80%">hi this code try</td></tr>");

        strHTML.Append("</table>");
        return strHTML.ToString();
    }

        public static string SendMailmsg(string body, string msgto, string cc, string msgfrom, string sub, string cond)
    {
        string status = "fail";
        try
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.Body = body;
            message.IsBodyHtml = true;
            if (cond == "notsendcc")
            {
                message.To.Add(msgto);
            }

            if (cond == "sendcc")
            {
                message.To.Add(msgto);
                message.CC.Add(cc);
            }
            message.From = new System.Net.Mail.MailAddress(msgfrom);
            message.Subject = sub;
            // message.ReplyTo = new MailAddress(repto.Trim());
            message.Priority = System.Net.Mail.MailPriority.High;
            SmtpClient SmtpMail = new SmtpClient();
            {
                SmtpMail.Host = "smtp.gmail.com";
                SmtpMail.Port = 587;
                //SmtpMail.Port = 25;
              //  SmtpMail.EnableSsl = false;
  SmtpMail.EnableSsl = true;
                SmtpMail.UseDefaultCredentials = true;
                SmtpMail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                SmtpMail.Credentials = new System.Net.NetworkCredential("email_id", "pass******");
                SmtpMail.Send(message);
            }
            message = null;
            status = "success";
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        return status;
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900