'object' does not contain a definition for 'xxxx'
Trying to send an email using a template.
Email.DefaultRenderer = new RazorRenderer();
data = new { FormReference = "7AM2TOVMSO", ClientReference = "E91XPU0UDC" }
var email = Email.From("[email protected],"xxx name") .To(recipient) .Subject("form data submitted") .UsingTemplate("Dear @Model.FormReference, You are totally @Model.ClientReference.", data, true);
sender.SendAsync(email);
It throws the 'object' does not contain a definition for 'ClientReference'. However the anonymous object with data has a property with that name.
Any idea what is going wrong?
This is sorted. Reason for this was instead of sending an ExpandoObject i sent a dynamic {} and for some reason it didn't work. When I create a ExpandoObject explicitly to fill my data, it works fine.
Then there is an issue with the SMTP send(). The SendCompleted shows an Error as below.
{System.Net.Mail.SmtpException: Syntax error, command unrecognized. The server response was: at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result) at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result) at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)}
" at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)\r\n at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result)\r\n at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)"
.......
I face the same problem because i used Fluent Email in separate class library and pass template as string and model as dynamic, after awhile i found the problem in this post which because "Anonymous objects are internal, which means their members are very restricted outside of the assembly that declares them. dynamic respects accessibility, so pretends not to be able to see those members. If the call-site was in the same assembly, I expect it would work.".
then i solve my problem by convert anonymous type to expandoobject using the following extension
public static class DynamicExtensions {
public static dynamic ToDynamic(this object value) {
IDictionary<string, object> expando = new ExpandoObject();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
expando.Add(property.Name, property.GetValue(value));
return expando as ExpandoObject;
}
}