如何通过Web API序列化和发送邮件

时间:2013-11-26 19:35:25

标签: c# json asp.net-web-api

当我尝试从C#对WebAPI执行POST操作时,我收到以下错误。

我得到的错误是:"Error getting value from 'replacementFallback' on 'System.Text.EncoderReplacementFallback'".

从C#调用API

EmailReportVM mailMsgRpt = new EmailReportVM();

    using (HttpClient client = new HttpClient())
             {
             // Add an Accept header for JSON format
             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
             //response = client.PutAsJsonAsync(InsertMailSendQueueapiURL, mailMsgRpt).Result;
             response = client.PostAsJsonAsync(InsertMailSendQueueapiURL, mailMsgRpt).Result;
             }

似乎发生了错误,因为Newtonsoft.Json无法将类型为MailMessage的名为mailMsg的属性序列化为Json。

我应该怎么做才能克服这个问题。

EmailReportVM:

 public class EmailReportVM 
    {

        public EmailReportVM ()
        {
          PhotoUrl = ConfigurationManager.AppSettings["PhotoUrl"];
        }

        public string from {get; set;};
        public string to {get; set;};
.....
        public MailMessage mailMsg { get; set; }

    }

mailMsg的正文如下:

<!DOCTYPE html>
<html>
<head>
    <title>Untitled Page</title>
    <style type="text/css">
        td{font-size:14px;font-family:verdana,arial,helvetica,sans-serif;}
    </style> 
</head>
<body>
    <table width="100%" border="0" cellspacing="3" cellpadding="3">
        <tr>
            <td>
                <strong>Your Agent Contact Information</strong>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr valign="top">
                        <td width="90%">
                            <table>
                                <tr>
                                    <td>
                                        <strong>Agent Name:</strong>
                                    </td>
                                    <td>
                                        Juan Miguel 
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong>Agent Phone:</strong>
                                    </td>
                                    <td>
                                        300-300-1100
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong>Agent Fax:</strong>
                                    </td>
                                    <td>
                                        800-700-2700
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong>E-mail Address:</strong>
                                    </td>
                                    <td>
                                        user1@dev.themls.com
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <br />

    <p style="font-size:14px;font-family:verdana,arial;">Check out this report.</p>
    <a href="http://localhost:65355/Stats/EmailedReports/AXoaIbi60eNsKNl_w3jr2Q2">
        <img alt="click here" src="http://www.thempd.com/images/click_here.gif" /></a> to view your report.  <br />

    <br />    
</body>
</html>

0 个答案:

没有答案