You must Sign In to post a response.
  • Category: .NET

    Need Email code using Asp.net c#

    Hi,Am using asp.net c# , when i click send button i want to send email by using code.i have a code but not working any buddy have idea or code ? need to send (gmail and webmail).
  • #768337
    Hi,

    We have huge number of examples for sending mail using C# applications.
    Basically we need SMTP to sent this mail to anywhere from the application.

    Initially we need to add Libraries to make it work.

    using System.Net;
    using System.Net.Mail;
    using System.Net.Mime;


    The SMTP server should be maintained and configured.

    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.google.com"); // Any server as you preferred



    The remaining codings will be the configuring of the mails like From,To and subjects etc.


    mail.From = new MailAddress("123@gmail.com");
    mail.To.Add("321@gamil.com");
    mail.Subject = Testing Application ;
    mail.Body = "Check the Report";
    Attachment attachment = new Attachment(filename);
    mail.Attachments.Add(attachment);



    Finally we can send the mail with send() option in s SMTP.

    SmtpServer .Send(mail);


    Thanks,
    Mani

  • #768346
    You can use given code snippet to send email using Gmail account and Gmail SMTP server in ASP.Net
     protected void SendEmail(object sender, EventArgs e)
    {
    using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
    {
    mm.Subject = txtSubject.Text;
    mm.Body = txtBody.Text;
    if (fuAttachment.HasFile)
    {
    string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
    mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
    }
    mm.IsBodyHtml = false;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Your Email sent.');", true);
    }
    }

    More useful reference: http://stackoverflow.com/questions/18326738/how-to-send-email-in-asp-net-c-sharp
    http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp


  • Sign In to post your comments