locked
Send Mail Using C# RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    using System.Net;
    using System.Net.Mail;

    public partial class _Default : System.Web.UI.Page
    {
        protected void SendEmail(object sender, EventArgs e)
        {
            string to = txtTo.Text;
            string from = txtEmail.Text;
            string subject = txtSubject.Text;
            string body = txtBody.Text;
            using (MailMessage mm = new MailMessage("gmail account","my personal account to send message on this email"))
            {
                mm.Subject = "txtSubject.Text";
                mm.Body = "Hello";
                
                mm.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential("gmail account", "password");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
                ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
            }
        }
    }
    • Moved by Kristin Xie Tuesday, January 6, 2015 1:45 AM move from C# forum
    Saturday, December 27, 2014 5:48 AM

Answers

  • This is related to WebForm application so that you can ask your question in http://forums.asp.net

    -----------------------------------------------

    Hope above help !

    Doanh

    • Proposed as answer by Cor Ligthert Saturday, December 27, 2014 10:22 AM
    • Marked as answer by Just Karl Tuesday, January 20, 2015 9:11 PM
    Saturday, December 27, 2014 9:19 AM

All replies

  • What's the error you're getting?

    This code snippet could help you.

    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    message.To.Add("luckyperson@online.microsoft.com");
    message.Subject = "This is the Subject line";
    message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
    message.Body = "This is the message body";
    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
    smtp.Send(message);

    How do I send mail using C#


    Happy Coding!

    Saturday, December 27, 2014 7:49 AM
  • This is related to WebForm application so that you can ask your question in http://forums.asp.net

    -----------------------------------------------

    Hope above help !

    Doanh

    • Proposed as answer by Cor Ligthert Saturday, December 27, 2014 10:22 AM
    • Marked as answer by Just Karl Tuesday, January 20, 2015 9:11 PM
    Saturday, December 27, 2014 9:19 AM