troubleshooting Question

how to send emails with console app

Avatar of SirReadAlot
SirReadAlot asked on
C#
17 Comments1 Solution295 ViewsLast Modified:
Hi experts,

was just wondering how to do this------------------->

I have a console app with two methods, one method can query a database and shows [email address] while the other sends an email.

how can i combine both so that for each [email address] that is read in method one will be sent an email.


===========================
using System;
using System.Web;
using System.IO;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;

namespace EmailSender
{
    /// <summary>
    /// Email Sender is a console application designed to send e-mails to mace new starters.
    /// </summary>
    class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            RunStoredProc();
         }

   #region Database Connection Code
         // run a simple stored procedure
        public static void RunStoredProc()
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
            try
            {
                // create and open a connection object
                conn = new
                SqlConnection("Database=xxxx;Data Source=xxxx;User ID=xxxxx;Password=xxxxx;");
                conn.Open();
                Console.WriteLine("Connection Successful");
                // create a command object identifying the stored procedure
                SqlCommand cmd = new SqlCommand("xxxxxxx", conn);
                //  set the command object so it knows to execute a stored procedure
                cmd.CommandType = CommandType.StoredProcedure;
                // execute the command
                rdr = cmd.ExecuteReader();
                // iterate through results, printing each to console
                while (rdr.Read())
                {
                    Console.WriteLine("t{0}\t{1}", rdr["ID"], rdr["Email"]);
                   // SendMail();
                }
            }
            finally
            {

                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
                Console.Read();
            }
        }
   #endregion

   #region SendMail Code    
        public static void SendMail()
        {
            //Configure basic infomation for an e-mail message
            string FromName = "xxx";
            string FromEmail = "xxxxx@xxx.co.uk";
            string Subject = "Sample Email with attachment";
            string ToName = "xxxxx";
            string ToEmail = "xxxxx@xxxx.xx.xx";
               
            //Assign the attachment path
            string FileName = Environment.CurrentDirectory + @"\Attachments\NewStarter.doc";
         
            //Create the email server host
            System.Net.Mail.SmtpClient
            smtp = new System.Net.Mail.SmtpClient("mail.xxxx.co.uk");

            //Create the MailMessage object
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

            //Assign from address
            msg.From = new System.Net.Mail.MailAddress(FromEmail, FromName);

            //Assign to address
            msg.To.Add(new System.Net.Mail.MailAddress(ToEmail, ToName));
         
            //assign subject, and body
            msg.Subject = Subject;
            msg.Body = "This is a test message with an attachment";
            msg.Priority = MailPriority.High;
         
            msg.Attachments.Add(new System.Net.Mail.Attachment(FileName));
            //Send the message with SmtpClient
                smtp.Send(msg);
        }
    }
   #endregion
}




Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 17 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros