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

    Need Email code using c#

    Hi,Am using 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

    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(""); // 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("");
    mail.Subject = Testing Application ;
    mail.Body = "Check the Report";
    Attachment attachment = new Attachment(filename);

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

    SmtpServer .Send(mail);


  • #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.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Your Email sent.');", true);

    More useful reference:

  • Sign In to post your comments