Thursday 22 October 2015

Send Mail using .Net and Sql Server



******************************  .Net *********************************************

try
{
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;

string emailFrom = "bhavdiptala@gmail.com";
string password = "****";
string emailTo = "bhavdip@dnkmail.in";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient(smtpAddress, portNumber);
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
catch (Exception ex)
{
}
******************************  Sql Server *********************************************
  1. Go to Object Explorer
  2. Management
  3. Right click on Database Mail and select “Configure Database Mail” as follows

  4. Next
  5. Select “Setup Database Mail by performing the following tasks” as follows
  6. Next
  7. Enter profile name = “SQL Profile” and description as follows
  8. Click on “Add” button and enter the following details. Always use your own email ID. Generally we have to use here the company email id. We have to raise a ticket to the mail server admin team to get the following details.
    1. Separate email id for SQL Server (This is From Email ID)
    2. SMTP server name
    3. Port number
    4. SSL feature should be enable or disable.
    Here I am using my personal email id. In Basic Authentication option enter the same email ID along with the valid password of the email ID.

  9. OK
  10. Next
  11. Under Manage Profile Security option make the profile as public by selecting checkbox and default as follows

  12. Next
  13. Accept the default settings for System Parameters as follows

  14. Next
  15. Finish

  16. Close.
USE msdb
GO
EXEC sp_send_dbmail @profile_name='SqlProfile',
@recipients='
bhavdip@dnkmail.in',
@subject='Test message',
@body='This is the body of the test message.'



No comments:

Post a Comment

SqlDataBaseLibrary

using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using AOS.Repository.Infrastructure; using S...