简体   繁体   中英

How to add items to list with different datatypes

I am working on a project that will be retrieving a list of emails to be sent out. I have code in a separate class to create the items that we need for emails:

public class EmailStructure
{
    public MailMessage Email { get; set; }
    public int MailId { get; set; }
    public int MailTypeId { get; set; }
}

I have code that returns the data and loops through each record in the dataset to create the new MailMessage , my problem is that when I try to add the MailMessage to my list, I get an error that says:

Argument type System.New.Mail.MailMessage is not assignable to parameter type 'EmailStructure'.

Obviously I am doing something wrong here but I cannot figure out what the problem is. My code is below:

class SendMails
{
    public List<EmailStructure> emailMessages = new List<EmailStructure>();
    public List<GetOutboundEmailsResult> emailResults = new List<GetOutboundEmailsResult>();

    public Extract()
    {
        // get data from DB
    }

    public void Transform()
    {
        if(emailResults.Any())
        {
            foreach (GetOutboundEmailsResult item in emailResults)
            {
                var emailBody = GetEmailBody(item);

                // create email this returns the type as MailMessage
                var email = emailHandler.ComposeNewEmail(
                    System.Configuration.ConfigurationManager.AppSettings["Mailbox"],
                    item.MailboxFrom,
                    string.Empty,
                    string.Empty,
                    emailBody.ToString(),
                    true,
                    "test");

                // add email message to List<MailMessage>
                // need to add MailMessage, MailId, MailTypeId 
                emailMessages.Add(email); // error is appearing here
            }
        }
    }

    public Route()
    {
        // send the emailMessages
        foreach(var row in emailMessages)
        {
            // need to use the MailId, MailTypeId here from the list
            emailHandler.SendNewEmail(row.Email)
        }
    }
}

How can I add the MailMessage, Maild and MailTypeId to my list? I need all of these items so I can send the emails in a method later in my process.

You're trying to add a MailMessage directly to emailMessages , when the latter holds EmailStructure s. Try something like:

emailMessages.Add(new EmailStructure()
{
    Email = email,
    MailId = 42,
    MailTypeId = 108
});

With whatever numbers are appropriate.

You are trying to add an object of type MailMessage to a list of type List<EmailStructure> . That's the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM