Click here to Skip to main content
16,010,392 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Issue I am having is when I call my wcf service from another c# app using the code below, I get the email service to send the needed email (it works), but the calling application with the code below captures an error (via try{}Catch{}) indicating the following: "Value cannot be null. Parameter name: Internal Error: The instance of the MessageContract cannot be null in http://tempuri.org/IEmailService/SendEmailResponse.". This error happens only when the calling application is sending an email without attachment, when sending an email with attachment though, there would be no error. Not sure how to resolve this issue.

C#
EmailServiceClient proxy = new EmailServiceClient();
           EmailMessage emailMsg = new EmailMessage();

           emailMsg.EmailParamHeaders = new DataContracts.Email();

           emailMsg.EmailParamHeaders.To = new string[1];
           emailMsg.EmailParamHeaders.CC = new string[1];

           emailMsg.EmailParamHeaders.From = tmpactivityDetails.FromEmailAddress;
           emailMsg.EmailParamHeaders.To[0] = tmpactivityDetails.ToEmailAddress;
           emailMsg.EmailParamHeaders.CC[0] = "";

           emailMsg.EmailParamHeaders.Subject = tmpactivityDetails.EmailSubject;
           emailMsg.EmailParamHeaders.Message = tmpactivityDetails.EmailBody;

           emailMsg.EmailParamHeaders.AttachmentFileName = null;

           proxy.SendEmail(emailMsg.EmailParamHeaders, Stream.Null);
           proxy.Close();


Here below is some code from my wcf service app showing you the EmailMessage contract:
SQL
namespace MessageContract
{


    [MessageContract]
    public class EmailMessage
    {
        [MessageHeader(MustUnderstand = true)]
        DataContracts.Email EmailParamHeaders;
        [MessageBodyMember(Order = 1)]
        public Stream EmailAttachment;
    }

    [MessageContract]
    public class ReturnValues   //Note: returning an array of error codes-if any
    {
        public string[] returnValues { get; set; }
    }
}


Here is the email contract:

C#
namespace DataContracts
{
    [DataContract(Name="Email")]
    public class Email
    {

        [DataMember(Order = 0, IsRequired =  false)]
        public string From { get; set; }
        [DataMember(Order = 1, IsRequired = true)]
        public string[] To { get; set; }
        [DataMember(Order = 2, IsRequired = false)]
        public string[] CC { get; set; }
        [DataMember(Order = 3, IsRequired = true)]
        public string Subject { get; set; }
        [DataMember(Order = 4, IsRequired = true)]
        public string Message { get; set; }
        [DataMember(Order = 5, IsRequired = false)]
        public string AttachmentFileName { get; set; }
    }
}


Let me know if u need more information, thanks.
Posted
Updated 20-Jul-12 14:08pm
v2
Comments
Kenneth Haugland 20-Jul-12 20:12pm    
Dont know what you did wrong, but I have used this code:
http://deepumi.wordpress.com/tag/send-email-from-silverlight/
and that worked fine.

tempuri means TEMPoraryURI. You should change it.

I think your contract needs to state that the EmailAttachment is optional, that's why it's blowing up.
 
Share this answer
 
Comments
Member 8714829 20-Jul-12 18:15pm    
Not sure how to state that and where, I tried to do that at the EmailMessage contract but I don't think I have the option to set the "public Stream EmailAttachment" to be optional, any ideas? And for the tempuri address it is done automatically when using svcutil.exe to package the wcf, how do I change it and what should it be? Thank you for your feedback.
Christian Graus 20-Jul-12 18:16pm    
honestly, I've never done WCF, but it seems to me like your contract says you require it, and you don't. Search your project for tempuri, yes, it's auto generated, but it's a placeholder, and should be changed to something that identifies your company.
Resolved issue which was caused by trace logging of null file name (unrelated).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900