Reputation: 247680
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.
Upvotes: 2
Views: 880
Reputation: 2812
You are trying to add an object of type MailMessage
to a list of type List<EmailStructure>
. That's the problem.
Upvotes: 0
Reputation: 14086
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.
Upvotes: 4