在silverlight中创建时事通讯

时间:2011-12-01 14:10:55

标签: c# silverlight wcf email newsletter

对于我的silverlight项目,我正在创建一个可以通过某个页面发送的新闻通讯。

为此,我有一个新闻通讯服务,可以在这里看到:

       public bool SendMail(string emailTo, string emailFrom, string msgSubject, string msgBody)
    {
        bool success = false;
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(emailFrom);
            msg.To.Add(new MailAddress(emailTo));
            msg.Subject = msgSubject;
            msg.Body = msgBody;
            msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; // Replace with your servers IP address
            smtp.Port = 465;
            smtp.EnableSsl = false;
            smtp.Send(msg);
            success = true;
        }

        catch(Exception e)
        {
            success = false;
        }

        return success;

    }

我要求服务使用以下代码发送邮件:

   public partial class Nieuwsbrief : UserControl
{
    IEnumerable<Inschrijvingen> ingeschrevenen = null;

    public Nieuwsbrief()
    {
        InschrijvingenServiceClient personclient = new InschrijvingenServiceClient();

        personclient.getInschrijvingenCompleted += new EventHandler<getInschrijvingenCompletedEventArgs>(personclient_getInschrijvingenCompleted);
        personclient.getInschrijvingenAsync();

        InitializeComponent();
    }

    private void btnSubmit_Click(object sender, RoutedEventArgs e)
    {
        NieuwsbriefServiceClient client = new NieuwsbriefServiceClient();

        //foreach (Inschrijvingen person in ingeschrevenen)
        //{
            client.SendMailAsync("test@gmail.com", "nieuwsbrief@ondernemersaward.be", txtSubject.Text, txtContent.Text);
        //}

        client.SendMailCompleted += new EventHandler<SendMailCompletedEventArgs>(client_SendMailCompleted);
    }

    void personclient_getInschrijvingenCompleted(object sender, getInschrijvingenCompletedEventArgs e)
    {
        ingeschrevenen = e.Result;
    }

    void client_SendMailCompleted(object sender, SendMailCompletedEventArgs e)
    {
        if (e.Error != null)
            txtContent.Text = "Mail has NOT send!" + e.Error.ToString();
        else
            MessageBox.Show(e.Result.ToString());
    }
}

但是当我按btnSubmit时没有任何反应,直到我关闭服务器然后我收到以下错误:

Mail has NOT send!System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
   --- End of inner exception stack trace ---
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at OndernemersAward.NieuwsbriefServiceReference.NieuwsbriefServiceClient.NieuwsbriefServiceClientChannel.EndSendMail(IAsyncResult result)
   at OndernemersAward.NieuwsbriefServiceReference.NieuwsbriefServiceClient.OndernemersAward.NieuwsbriefServiceReference.NieuwsbriefService.EndSendMail(IAsyncResult result)
   at OndernemersAward.NieuwsbriefServiceReference.NieuwsbriefServiceClient.OnEndSendMail(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

我希望有人可以帮助我, 谢谢:))

1 个答案:

答案 0 :(得分:1)

在你的catch语句中这样做:

catch (Exception e)
{   //<---- Put the break point here.

}

然后你可以看到'e',看看为什么会失败。

我的猜测是smtp.Send失败了,它可以抛出a lot of exceptions,所以我不敢猜测是哪一个!

编辑:此外,在其中显示“//替换为您的服务器IP地址”,您是否将其替换为服务器的IP地址?我首先假设您发布了演示代码,但我想确定。

嗯,这是你的问题! Gmail允许您将它们用作smtp服务器。使用它们:http://www.geekzone.co.nz/tonyhughes/599。您必须将smtp.Port更改为465,将smtp.Host更改为smtp.gmail.com。这需要身份验证,因此请创建一个Gmail帐户。