473,382 Members | 1,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Not getting status event from SmtpClient

I'm trying to send emails using the SmtpClient class and, while the messages
get sent, I can't seem to get any status information to that effect. I've
hooked in to the SendCompleted event, but it never fires. Can anyone suggest
why or tell me how to find out whether the email was sent or not?

The code I'm using is as follows (credentials removed to protect the
guilty):

using System;
using System.Text;
using System.Net.Mail;

namespace SendStuffBySmtp
{
public delegate void MailSendResults(String resultsMessage);

class Emails
{
public event MailSendResults MailResults;

private MailAddress fromAddress = new
MailAddress("me@my.email.address", "My Name");
private SmtpClient smtp = new SmtpClient("my.isp.smtp.address");
private String token;

public Emails()
{
smtp.UseDefaultCredentials = true;
// Hook in to the send completed event. We want to know when
this finishes
smtp.SendCompleted += new
SendCompletedEventHandler(smtp_SendCompleted);
}
public void SendEmail(string emailTitle, string emailText, string
recipient)
{
MailMessage msg = new MailMessage(fromAddress, new
MailAddress(recipient));
msg.IsBodyHtml = true;
msg.Body = emailText;
msg.BodyEncoding = System.Text.Encoding.UTF8;

msg.Subject = emailTitle;
msg.SubjectEncoding = System.Text.Encoding.UTF8;

msg.From = fromAddress;
msg.ReplyTo = fromAddress;
msg.Sender = fromAddress;

token = recipient;
smtp.Send(msg);
}

void smtp_SendCompleted(object sender,
System.ComponentModel.AsyncCompletedEventArgs args)
{
if (MailResults != null)
{
// Get the unique identifier for this asynchronous
operation.
string resultsText = null;

if (args.Cancelled)
{
resultsText = String.Format("[{0}] Send canceled.",
token);
}
if (args.Error != null)
{
resultsText = String.Format("[{0}] {1}", token,
args.Error.ToString());
}
else
{
resultsText = String.Format("[{0}] Message sent.",
token);
}

MailResults(resultsText);
}
}
}
}

I've put a breal point in the smtp_SendCompleted event code but it never
gets called.

I also tried using SendAsync() but that didn't fire the SendCompleted event
either.

Thanks
Steve
Jun 27 '08 #1
1 3880
Don't worry about it, I restructured the code to use exceptions instead.

Steve
"Steve Barnett" <no****@nodomain.comwrote in message
news:uD**************@TK2MSFTNGP03.phx.gbl...
I'm trying to send emails using the SmtpClient class and, while the
messages get sent, I can't seem to get any status information to that
effect. I've hooked in to the SendCompleted event, but it never fires. Can
anyone suggest why or tell me how to find out whether the email was sent
or not?

The code I'm using is as follows (credentials removed to protect the
guilty):

using System;
using System.Text;
using System.Net.Mail;

namespace SendStuffBySmtp
{
public delegate void MailSendResults(String resultsMessage);

class Emails
{
public event MailSendResults MailResults;

private MailAddress fromAddress = new
MailAddress("me@my.email.address", "My Name");
private SmtpClient smtp = new SmtpClient("my.isp.smtp.address");
private String token;

public Emails()
{
smtp.UseDefaultCredentials = true;
// Hook in to the send completed event. We want to know when
this finishes
smtp.SendCompleted += new
SendCompletedEventHandler(smtp_SendCompleted);
}
public void SendEmail(string emailTitle, string emailText, string
recipient)
{
MailMessage msg = new MailMessage(fromAddress, new
MailAddress(recipient));
msg.IsBodyHtml = true;
msg.Body = emailText;
msg.BodyEncoding = System.Text.Encoding.UTF8;

msg.Subject = emailTitle;
msg.SubjectEncoding = System.Text.Encoding.UTF8;

msg.From = fromAddress;
msg.ReplyTo = fromAddress;
msg.Sender = fromAddress;

token = recipient;
smtp.Send(msg);
}

void smtp_SendCompleted(object sender,
System.ComponentModel.AsyncCompletedEventArgs args)
{
if (MailResults != null)
{
// Get the unique identifier for this asynchronous
operation.
string resultsText = null;

if (args.Cancelled)
{
resultsText = String.Format("[{0}] Send canceled.",
token);
}
if (args.Error != null)
{
resultsText = String.Format("[{0}] {1}", token,
args.Error.ToString());
}
else
{
resultsText = String.Format("[{0}] Message sent.",
token);
}

MailResults(resultsText);
}
}
}
}

I've put a breal point in the smtp_SendCompleted event code but it never
gets called.

I also tried using SendAsync() but that didn't fire the SendCompleted
event either.

Thanks
Steve


Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
6
by: theWizard1 | last post by:
Using VisualStudio 2005, asp.net 2.0: What do I put in my Web.Config to avoid hard coding the: myserveraddress.com? The following shows how I have hard coded it without having the value in the...
6
by: rekaeps | last post by:
We are developing an ASP.NET 2.0 (C#) application, and I'm having troubles sending e-mail from the server when accessing the web site from a separate client computer. Also, in the same scenario,...
10
by: David Thole | last post by:
Hey all, I'm still very new at all this, but am going through the ASP.net 2.0 unleashed book, first chapter and trying to program my own little form emailer for fun. I tried following the code...
1
by: Sachin1234 | last post by:
Hi , I am getting the error in system log that " The remote server did not respond to a connection attempt." i am using following code for sending mail /**************** Work for...
4
by: AshishMishra16 | last post by:
HI friends, I am using the Flex to upload files to server. I m getting all the details about the file, but I m not able to upload it to Server. Here is the code i m using for both flex & for...
6
by: =?Utf-8?B?VG9yc3Rlbg==?= | last post by:
Hi, I have a problem sending mails with the SmtpClient class. It's strange - after I boot the pc and start the program I can send mails many times. After I close the program and start it again...
7
by: Rob Dob | last post by:
The following code is giving me a timeout problem., no matter what I do I can't send a piece of mail using .net2.0 System.Net.Mail.SmtpClient via port 465 and using ssl, if however I try using...
2
by: btcoder | last post by:
Hi, my jsp page uses sun.net.smtp.SmtpClient to send email. It worked fine until the hosted location was moved to another server. Now it generates the sun.net.smtp.SmtpProtocolException and the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.