473,491 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3885
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
7672
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
10474
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
2506
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
2978
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
1387
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
6193
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
5472
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
22837
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
3818
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...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.