无法从字符串转换为system.collection.generic.list [英] cannot convert from string to system.collection.generic.list

查看:97
本文介绍了无法从字符串转换为system.collection.generic.list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



我想把测试信息发送给人员列表,但是我希望每次都能对这些消息进行个性化。但是我已经完成了这个消息。使用我的代码得到两个错误:



1:Mailing.Mailing.SendEmail的最佳重载方法匹配(字符串,字符串,System.Collections.Generic.List



2:参数3:无法从'string'转换为'System.Collections.Generic.List



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

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

推荐答案

在你的 SendTestEmail 代码中,你建立了一个人的列表逐步生成个性化消息。



在您第一次通过此消息时,您将设置该消息为亲爱的Joe并致电 SendEmail 方法。



但您也将人员列表传递给该方法,并且在该方法(SendEmail)中明确说明
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
foreach (Person person in people)
                mailing.Bcc.Add(person.EmailAddress);

换句话说,将当前文本(亲爱的Joe)的副本发送给列表中的每个人!



如果你删除这两行,那么你应该解决这个问题,但你从来没有真正设置过谁ail将被发送到

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);


用<更改此行br $> b $ b

Change this line with

SendEmail(emailSubject, emailText, p.EmailAddress );





with





with

SendEmail(emailSubject, emailText,p);







这将解决上述两个问题




this will solve both the above problems


这篇关于无法从字符串转换为system.collection.generic.list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆