[RESOLVED]Send mails to users

Hi, 

I have a table called users that contains emails

users (ID, email, role)

I would like to send emails to  users using Gmail, 

Any help please ?

Typically, you can use the SmtpClient class to handle sending e-mails through ASP.NET : 

//Create an SMTPClient that will handle sending your email
SmtpClient client = new SmtpClient("80.97.*.*", 25);
client.UseDefaultCredentials = false;

//Set up your Credentials of where to send it from
client.Credentials = new NetworkCredential("username", "password");

//Build your message
string toAddress = "to@domain.com";
string fromAddress = "from@domain.com";
string messageSubject = "Your Subject";
string body = "Body of your email here";

//Build your actual message to send
MailMessage mm = new MailMessage(toAddress, fromAddress, messageSubject, body);

//Set up your encoding
mm.BodyEncoding = UTF8Encoding.UTF8;

//Send your message
client.Send(mm);

Depending on your needs and for the sake of your WebServer, if you have thousands of emails to send, it preferrable to use some type of batch sending (queue a batch of recipients to send to, rest the server and repeat) to avoid being blacklisted
as a spammer.

You will need to look into some other methods of sending massive amounts of emails (such as performing them in batches with delays in between each batch rather than sending them all at once or using a specific service to handle this for you). If
you have a dedicated server, you’ll want to implement the delays and batch sending through a Windows Service.

If you don’t have a dedicated server, then you might want to consider using a third-party to handle sending your messages such as Amazon’s SES (Simple Email
Service
) which you can read about how to implement in your .NET Application here
 or

MailChimp
.

References

Hi,

To send e-mail you need to configure SMTP server.

To Configure a SMTP refer this link : Configuring SMTP Email(IIS 7)

To send the Email you can use the below code

 C# use  System.Net.Mail namespace for sending email.

Add the below code inside your button click event

//Set email user name - Change this as per your settings
            const string username = "testuser@googlemail.com";
            //Set the email password - Change this as per your settings
            const string password = "xxxxxxxxx";
            SmtpClient smtpclient = new SmtpClient();
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            //Set the email from address of mail message -  Change this as per your settings
            MailAddress fromaddress = new MailAddress("testuser@googlemail.com");
            //Set the email smtp host -  Change this as per your settings
            smtpclient.Host = "smtp.gmail.com";
            //Set the email client port -  Change this as per your settings
            smtpclient.Port = 587;
            mail.From = fromaddress;
            //Adding email id of receiver link
            mail.To.Add("testuser@googlemail.com");
            //Set the email subject
            mail.Subject = ("Test Subject");
            mail.IsBodyHtml = true;
            //Set the email body - Change this as per your logic
            mail.Body = "This is test Mail";
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
            try
            {
                //Sending Email
                smtpclient.Send(mail);
                Response.Write("<B>Email Has been sent successfully.</B>");
            }
            catch (Exception ex)
            {
                //Catch if any exception occurs
                Response.Write(ex.Message);
            }

Above smtp settings are for sample, you may need to change this as per your smtp server settings.

You can also check the below links

Rion Williams

string toAddress = "to@domain.com";

How can I write  :

string toAddress = "select email from User";

Well using a SQL statement there wouldn’t accomplish anything.

You could however, create a SqlConnection and use a DataReader to read through each of the addresses in the database and send a message that way : 

using(SqlConnection sqlConnection = new SqlConnection("Your Connection String"))
{
     // Open your connection
     sqlConnection.Open();

     // Create a SQL Command to execute
     using(SqlCommand sqlCommand = new SqlCommand("SELECT Email FROM User",sqlConnection))
     {
           // Execute your query into a datareader and read through your e-mail values
           using(SqlDataReader sqlReader = sqlCommand.ExecuteReader())
           {
                 // While there are records, read
                 while(sqlReader.Read())
                 {
                       // Grab your email from the current record and process the email
                       var emailAddress = sqlReader.GetString(0);

                       // Place your code to send to that email address here
                 }
           }
     }
}

Rion Williams

using(SqlConnection sqlConnection = new SqlConnection("Your Connection String"))

I have tried this 

Using sqlConnection As New SqlConnection("Data Source=SALIM\SQLEXPRESS;Initial Catalog="C:\USERS\SALIM\DESKTOP\ONDA ODESK\APP_DATA\ASPNETDB.MDF")

But couldn’t connect

var emailAddress = sqlReader.GetString(0);

                       // Place your code to send to that email address here



Can you provide the code please ?

You likely just need to escape your connection string :

Using sqlConnection As New SqlConnection("Data Source=SALIM\SQLEXPRESS;Initial Catalog=""C:\USERS\SALIM\DESKTOP\ONDA ODESK\APP_DATA\ASPNETDB.MDF""")

With regards to the e-mail code, you would use the code that I provided earlier (or another solution that you found) :

// Create an SMTPClient that will handle sending your email
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;

// Set up your Credentials of where to send it from
client.Credentials = new NetworkCredential("username", "password");

// Build your message
string fromAddress = "from@domain.com";
string messageSubject = "Your Subject";
string body = "Body of your email here";

using(SqlConnection sqlConnection = new SqlConnection("Your Connection String"))
{
     // Open your connection
     sqlConnection.Open();

     // Create a SQL Command to execute
     using(SqlCommand sqlCommand = new SqlCommand("SELECT Email FROM User",sqlConnection))
     {
           // Execute your query into a datareader and read through your e-mail values
           using(SqlDataReader sqlReader = sqlCommand.ExecuteReader())
           {
                 // While there are records, read
                 while(sqlReader.Read())
                 {
                       // Grab your email from the current record and process the email
                       var emailAddress = sqlReader.GetString(0);
                        
                       // Build your message and send it
                       MailMessage mm = new MailMessage(emailAddress, fromAddress, messageSubject, body);

                       //Set up your encoding
                       mm.BodyEncoding = UTF8Encoding.UTF8;

                       //Send your message
                       client.Send(mm);
                 }
           }
     }
}

Leave a Reply