Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send mail through asp.net form using C#.Net. I have written the code as below:

using System.Net.Mail;

 protected void Button1_Click(object sender, EventArgs e)
   {
       System.Net.Mail.MailMessage msg = new

           System.Net.Mail.MailMessage(); //create the message

       //msg.To.Add("balajisathe@gmail.com");

       msg.To.Add(TextBox1.Text);
       msg.From = new MailAddress(TextBox2.Text,

       txtusername.text, System.Text.Encoding.UTF8);

       msg.Subject = TextBox3.Text;
       msg.SubjectEncoding = System.Text.Encoding.UTF8;
Posted
Updated 5-Oct-10 21:21pm
v2
Comments
thatraja 6-Oct-10 2:26am    
Incomplete code....Also mention the error message which occurred while you have tried to sent mails. So edit your question & update the required details.
Abhishek Sur 6-Oct-10 4:55am    
Yes, it is important to specify the actual exception before anybody can help you.

Example ->

public static class MailUtility
    {
        public static void SendEmail(string sender, string receiver, string subject, string body)
        {
            MailMessage message = new MailMessage();
            message.From = new System.Net.Mail.MailAddress(sender, "ADMIN");
            message.To.Add(receiver);
            message.IsBodyHtml = true;

            message.Body = body;
            message.Subject = subject;

            SmtpClient smtp = new SmtpClient();
            try
            {
                smtp.Send(message);
            }
            catch (Exception)
            {

            }

            message = null;
        }
    }


webconfig:

<system.net>
		<mailsettings>
			<smtp
				network host="127.0.0.1" username="" password="" port="25" >
			</smtp>
		</mailsettings>
	</system.net>
</system.web>
 
Share this answer
 
v4
C#
using System.Net.Mail;
protected void Button1_Click(object sender, EventArgs e)
{
      string sendFrom = "yourmailaddress@gmail.com";
      string toAddress = "tomailaddress@gmail.com";
      string subject = "Test";
      string mailBody = "Test";
      SmtpClient client = new SmtpClient();            
      client.EnableSsl = true;
      MailMessage message = new MailMessage(sendFrom, toAddress,              subject, mailBody);
      message.IsBodyHtml = true;
      client.Send(message);
}    


web.config

XML
<system.net>
        <mailsettings>
            <smtp from="yourmailaddress@gmail.com"  deliveryMethod="Network">
                 <network host="smtp.gmail.com" port="25" userName="yourmailaddress@gmail.com" password="test123" />
      </smtp>

        </mailsettings>
    </system.net>
</system.web



If this helped you then Please vote.
 
Share this answer
 

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