Schoof
Schoof

Reputation: 2855

Creating newsletter in silverlight

For my silverlight project I'm creating a newsletter which can be send through a certain page.

For this I have a newsletter service which can be seen here:

       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;

    }

And I ask the service to send the mail with the following code:

   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("[email protected]", "[email protected]", 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());
    }
}

However when I press btnSubmit nothing happens, untill I shut down the server and then I get the following error:

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)

I hope somebody can help me, Thanks :)

Upvotes: 0

Views: 268

Answers (1)

DanTheMan
DanTheMan

Reputation: 3277

In your catch statement do this:

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

}

You can then see 'e' and see why this is failing.

My guess is that it's smtp.Send that's failing, and it can throw a lot of exceptions, so I won't venture to guess which one!

EDIT: Also, where it says "// Replace with your servers IP address", did you replace it with your server's IP address? I first assumed you posted demo code, but I want to be sure.

Well, this is your problem! Gmail lets you use them as an smtp server. Use them: http://www.geekzone.co.nz/tonyhughes/599. You have to change your smtp.Port to 465 and the smtp.Host to smtp.gmail.com. This requires authentication, so go create a gmail account.

Upvotes: 1

Related Questions