-3

I am a beginner in SQL and ASP.NET, basically i'm creating a website in that there is a contact us page. In that there are four textbox (Name, Email Address,Subject & Your Message). What ever i type in the text box it stored in the table's database in SQL. So now how to send the data (which is present in table) to gmail account.

mohammed nabil
  • 95
  • 2
  • 13
  • 1
    Sending an email to Gmail is no different to sending an email to any other mail provider; use [`sp_send_dbmail`](https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql?view=sql-server-ver15). Where that email is delivered to (gmail, Outlook, Yahoo, a company hosted STMP server) doesn't matter. SQL Server will simply connect to the email account you have configured and then that mail server does the rest of the work. – Thom A Sep 02 '20 at 12:01

1 Answers1

-1

Prerequisites: SMTP server (Gmail, exchange, any other)

Options

  • Send mail from c#
  • Send mail in database (wouldn't recommend as SQL Server is expensive in licensing)

How to do it from c#:

using(var smtpClient = new SmtpClient("smtp.gmail.com", 587)
{
    EnableSsl = true,
})
{
    var credentials = new NetworkCredential("your-sender-account@gmail.com", "your-sender-passworder"); // Don't store you password in clear text
    var smtpClient.Credentials = credentials;

    var to = new MailAddress("to-email@gmail.com");
    var from = new MailAddress("from-email@gmail.com");
    var mailMessage = new MailMessage(from, to)
    {
        Subject = "Your subject",
        Body = "Your content"
    };

    smtpClient.Send(mailMessage);
}

If you use gmail as your smtp server, you would need to allow less secure apps in your gmail settings, of the account used in the NetworkCredintial

Documentation SmtpClient

Preben Huybrechts
  • 5,853
  • 2
  • 27
  • 63
  • 1
    If i use this c# code for example i use in "NetworkCredential("xyz@gmail.com,"pass"), var to=(xyz@gmail.com), var from=(abc@gmail.com)". And without any issue i receive an email but problem is in the email if i expand the details it is showing "From:xyz@gmail.com To:xyz@gmail.com" .Although i type abc@gmail.com in "var from" – mohammed nabil Sep 02 '20 at 12:20