在silverlight中创建时事通讯
本文关键字:创建 silverlight | 更新日期: 2023-09-27 18:16:34
对于我的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)
我希望有人能帮助我,谢谢:)
在catch语句中这样做:
catch (Exception e)
{ //<---- Put the break point here.
}
你可以看到'e',看看为什么失败。
我猜是smtp.Send
失败了,它可以抛出很多异常,所以我不会冒险猜测哪一个!
编辑:另外,它说"//替换你的服务器的IP地址",你替换它与你的服务器的IP地址吗?我一开始以为你发布了演示代码,但我想确认一下。
好吧,这就是你的问题!Gmail允许你使用它们作为smtp服务器。使用它们:http://www.geekzone.co.nz/tonyhughes/599。您必须更改您的smtp。端口为465和smtp。主机到smtp.gmail.com。这需要认证,所以去创建一个gmail帐户。