Hi guys,

I had a test the other day and missed a question out because it confused the hell out of me. I dont know if it was a badly written question or just me. Heres the program below:

public static class Mailing
{
public static 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 emailText = "Dear user, this is an email to inform you
that your training course on the 1st of January has been cancelled";
SendEmail(emailSubject, emailText, people);
}
private static 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;
}
string FirstName;
string LastName;
string EmailAddress;
}

The question was - adapt the code to include person specific information in the email, you can re-write any or
all of the supplied code to do this.

Any idea what I was meant to do guys?

Recommended Answers

All 8 Replies

Personalize the message. In emailText replace the word "user" with each person's first name. Use String.Replace

I would suspect that the instructor wants you to loop through the list of persons and use the first name in place of 'user'.

public static 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";
    foreach(Person p in people)
    {
        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);
    }
}

SenMail would have to be modified of course to accept only one email address at a time, since each email will have a personalized message. I think this would be preferrable to replacing 'user' each time since that will create a new string for each user plus the template.

You will also need to change the protection level of "FirstName", "LastName", and "EmailAddress" (in person).

Otherwise the following line will throw a compiler error:

mailing.Bcc.Add(person.EmailAddress);

Accessibility Levels (C# Reference)

Members of: class

Default member accessibility: private

tried changing the protection level, but still producing there error. reading that link it should be fine. private should has accessibility public or am I wrong?

got it working now. So if you dont declare protection it is default set to private?

So if you dont declare protection it is default set to private?

Enumerations and interfaces are public by default when nested in a class. Top level types default to internal. Everything else is private by default.

tinstaafl

changing the sendmail gives two erros. 1 - cannot convert from "string" to System.collection.generic.list<mailing.mailing.persons>

2- The best overloaded method match for 'Mailing.Mailing.SendEmail(string, string, System.Collections.Generic.List<Mailing.Mailing.Person>)' has some invalid argument

In my post I mentioned changing the sub routine to accept only one email address. This means changing it from list to string

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.