Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to send an E-mail using ASP .net (Web Forms) using smtp server
Posted

C#
string from = me@gmail.com; //Replace this with your own correct Gmail Address

string to = you@gmail.com //Replace this with the Email Address to whom you want to send the mail

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
 mail.To.Add(to);
 mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password

 client.Credentials = new System.Net.NetworkCredential(from, "Password");

client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
       try
        {
            client.Send(mail);
        }
        catch (Exception ex) 
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty; 
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
   HttpContext.Current.Response.Write(errorMessage );
        }
 
Share this answer
 
v2
Thank you for your question,

C#
using System.Net;
using System.Net.Mail;


C#
MailMessage msg = new MailMessage();

msg.From = new MailAddress(from);
msg.To.Add(to);
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;

SmtpClient smtpClient = new SmtpClient(YourSMTPServer, Port );

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;smtpClient.Credentials = new NetworkCredential(UserId, Password);
        
//smtpClient.EnableSsl = true;  //if you use SSL then true

if (msg.To.Count > 0)
{
   smtpClient.Send(msg);                    
}



Thanks,
Mamun
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
Try this method
C#
public static Boolean SendingMail(string From, string To, string Subject, string Body)
    {
        
            try
            {
                MailMessage m = new MailMessage("Test<sender@gmail.com>", To);
                m.Subject = Subject;
                m.Body = Body;
                m.IsBodyHtml = true;
                m.From = new MailAddress(From);

                m.To.Add(new MailAddress(To));
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "mail.google.com";
              
                NetworkCredential authinfo = new NetworkCredential("test@gmail.com", "te1234");
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = authinfo;
                smtp.Send(m);
                return true;

               


            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
Share this answer
 
C#
System.Net.Mail.MailMessage obj = new System.Net.Mail.MailMessage();
       

     SmtpClient serverobj = new SmtpClient();
            serverobj.Credentials = new NetworkCredential("anilmeets4u@gmail.com", "yourpassword");
            serverobj.Port = 587;
            serverobj.Host = "smtp.gmail.com";
            serverobj.EnableSsl = false;
            obj = new System.Net.Mail.MailMessage();
            obj.From = new MailAddress("anilmeets4u@gmail.com", "Google.com", System.Text.Encoding.UTF8);
            obj.To.Add(ASPxtxtToUser.Text);
            obj.CC.Add(ASPxTexttoCc.Text);
            obj.Priority = System.Net.Mail.MailPriority.High;
            obj.Subject = txtSubject.Text;
            string date = DateTime.Now.ToString();
            obj.Body = ASPxMemo1.Text;
            serverobj.Send(obj);
            lblsend.Text = "Your Message Send Sucessfully";



Try this.
 
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