Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hey guys,

I want to send a test message out to a list of people, however I want the message personalized each time.This ive done however im getting two errors in with my code:

1:The best overloaded method match for Mailing.Mailing.SendEmail(string, string, System.Collections.Generic.List

2: Argument 3: cannot convert from 'string' to 'System.Collections.Generic.List

C#
public void SendTestEmail()
        {
            List<Person> people = new List<Person>();

            

            //people.Add(new Person("Joe", "Bloggs", "Joe.Bloggs@foo.bar"));
           people.Add(new Person("John", "Smith", "John.Smith@foo.bar"));
           // people.Add(new Person("Ann", "Other", "Ann.Other@foo.bar"));
            string emailSubject = "Training cancellation notification";
            string emailFail = "Message did not send, please check log file";

            try
            {
                foreach (Person p in people)
                {

                    //personalise message for each person, replacing "user" with firstname
                    string emailText = ("Dear " + p.FirstName +
                    " this is an email to inform you that your training course on the 1st of January has been cancelled");

                    SendEmail(emailSubject, emailText, p.EmailAddress );

                }
            }
            catch (Exception e)
            {
                Logger(e.ToString()); // catch exception
                Console.WriteLine(emailFail);

            }
        }

        private void SendEmail(string emailSubject, string emailText,
        List<Person> people)
        {

            string emailHost = "smtp.foo.bar";
            MailAddress fromAddress = new MailAddress("system@foo.bar");
            MailMessage mailing = new MailMessage();
            mailing.From = fromAddress;
            mailing.Subject = emailSubject;
            mailing.To.Add(fromAddress);
            foreach (Person person in people)
                mailing.Bcc.Add(person.EmailAddress);
            
            mailing.Body = emailText;
            SmtpClient client = new SmtpClient(emailHost);

            client.Send(mailing);
        }


        public class Person
        {
            public Person(string firstName, string lastName, string emailAddress)
            {
                FirstName = firstName;
                LastName = lastName;
                EmailAddress = emailAddress;
            }
            // changed protection level in order for mailing.Bcc.Add to attain accessibilty
            public string FirstName;
            public string LastName;
            public string EmailAddress;
        }


Any idea what i must do to resolve it?

Cheers guys
Posted
Updated 13-Jul-14 9:58am
v2
Comments
On which line?
addy25 13-Jul-14 16:42pm    
SendEmail(emailSubject, emailText, p.EmailAddress ) is the problem
[no name] 13-Jul-14 16:18pm    
p.EmailAddress is a string not a List and your SendEmail method is expecting for you to pass it a List.
addy25 13-Jul-14 16:45pm    
I dont really want to change this list to string[]
[no name] 13-Jul-14 18:17pm    
Then don't

In your SendTestEmail code you have built up a list of people which you step through to generate the personalised message.

On your first pass through this you are going to set up that message as "Dear Joe" and call the SendEmail method.

But you are passing the list of people into that method as well, and within that method (SendEmail) you explicitly say
C#
foreach (Person person in people)
                mailing.Bcc.Add(person.EmailAddress);
In other words, send a copy of the "current" text (Dear Joe) to everyone in the list!

If you remove those two lines then you should resolve the issue but you never really set up who the email is to be sent to anyway
mailing.To.Add(fromAddress);
is sending it to the hard-code from address.

You would be better off doing some rethinking of your design - stick to certain principles, for example a method should really do one thing and one thing only.

A hint to make the personalisation a little tidier ... set up the text once but with a placeholder for the name e.g.
string emailText = ("Dear {0} \n\nThis is an email to inform you that your training course on the 1st of January has been cancelled");
Then to personalise the text you can do something like
emailText = String.Format(emailText, p.FirstName);
 
Share this answer
 
Comments
addy25 17-Jul-14 9:49am    
thank you.
Change this line with

C#
SendEmail(emailSubject, emailText, p.EmailAddress );


with

C#
SendEmail(emailSubject, emailText,p);



this will solve both the above problems
 
Share this answer
 
Comments
addy25 14-Jul-14 3:44am    
does the same thing I was having a problem, sending "dear joe....." to john and sue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900