Link to home
Start Free TrialLog in
Avatar of newjeep19
newjeep19Flag for United States of America

asked on

How to send emails using C# code

I have an C# console application that quieres a database to get the names of sales people in different departments to send them emails. However the emails are not being sent because the TO and CC field are not in the correct email format.
EX: Sales prerson name: John Smith -> from Query in stored proc
John Smith needs to be in this format so that he can get the email: jsmith@companyA.com.

What I am currently doing is get the name and then adding the @companyA.com which is not the correct format to send and email.
My Code is listed below --- Please help!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;

namespace EmailAEConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("Data Source=ServerName;Initial Catalog=DataBaseName;User ID=ID;Password=Password");
            SqlCommand command = new SqlCommand("StoredProcName", conn);

            try
            {
                command.CommandType = CommandType.StoredProcedure;

                //Add data to DataTable
                DataTable dtbItems = new DataTable();
                conn.Open();

                using (SqlDataAdapter sda = new SqlDataAdapter(command))
                    sda.Fill(dtbItems);
                {
                    //GATHER OPP DATA
                    {
                        //Loop thru all the properties
                        foreach (DataRow row in dtbItems.Select())
                        {
                            string oppSalesPersonTeamAName = Convert.ToString(row["salespersonATeamName"]);
                            string oppSalesPersonATeamManagerName = Convert.ToString(row["salespersonAManagerName"]);
                            string oppIAMName = Convert.ToString(row["SalesPersonTeamBName"]);
                           ["estimatedclosedate"]);

                            {
                                //****** NEED TO ADD "FOR EACH HERE ******
                                MailMessage mailAE = new MailMessage();

                                mailAE.From = new MailAddress("autogeneratedsystem@companyA.com"); 
                                mailAE.To.Add(oppSalesPersonTeamAName + "@companyA.com");
                                mailAE.To.Add(oppSalesPersonTeamBName + "@companyA.com");
                                mailAE.CC.Add(oppSalesPersonTeamAManagerName + "@companyA.com");
                                mailAE.Subject = " ****Warning****  Date Close (PO In) is estimated to close in the next 5 days: " + oppEstCloseDate.ToShortDateString() + "****Warning****";
                                mailAE.IsBodyHtml = true;
                                mailAE.BodyEncoding = System.Text.Encoding.UTF32;
                                mailAE.Body += "<br>Account Executive: " + oppSalesPersonTeamAName ;
                                mailAE.Body += "<br>IAM: " + oppSalesPersonTeamBName ;
                                mailAE.Body += "<br>Estimated Close Date: " + oppEstCloseDate.ToShortDateString();
                                mailAE.Body += "<br>";

                                SmtpClient s = new SmtpClient();
                                MailMessage m = new MailMessage();
                                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                                smtp.Host = "smtphostname";
                                smtp.Port = 25;
                                smtp.Send(mailAE);
                            }

                              conn.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR MESSAGE: " + ex.Message);
                conn.Close();
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of newjeep19

ASKER

Works great thanks for the help......still new to C#