Solved

How to send emails using C# code

Posted on 2013-01-17
2
288 Views
Last Modified: 2013-01-17
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

0
Comment
Question by:newjeep19
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38788249
so, you need to transform "John Smith" into "jsmith" ?

you could do this in the stored procedure or in the c# code, nothing too complex?
find the first space, and from that part:
* left to it, take only the first character, lowercase
* all right to it, lowercase
could be done with regular expression or with "brute force" code:
string short_name = oppSalesPersonTeamAName.Substring(0,1).ToLower() + oppSalesPersonTeamAName.Substring(oppSalesPersonTeamAName.IndexOf(" ")+1).ToLower();

Open in new window

0
 

Author Comment

by:newjeep19
ID: 38788829
Works great thanks for the help......still new to C#
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now