473,549 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ 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.addre ss", "My Name");
private SmtpClient smtp = new SmtpClient("my. isp.smtp.addres s");
private String token;

public Emails()
{
smtp.UseDefault Credentials = true;
// Hook in to the send completed event. We want to know when
this finishes
smtp.SendComple ted += new
SendCompletedEv entHandler(smtp _SendCompleted) ;
}
public void SendEmail(strin g emailTitle, string emailText, string
recipient)
{
MailMessage msg = new MailMessage(fro mAddress, new
MailAddress(rec ipient));
msg.IsBodyHtml = true;
msg.Body = emailText;
msg.BodyEncodin g = System.Text.Enc oding.UTF8;

msg.Subject = emailTitle;
msg.SubjectEnco ding = System.Text.Enc oding.UTF8;

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

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

void smtp_SendComple ted(object sender,
System.Componen tModel.AsyncCom pletedEventArgs 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.ToSt ring());
}
else
{
resultsText = String.Format("[{0}] Message sent.",
token);
}

MailResults(res ultsText);
}
}
}
}

I've put a breal point in the smtp_SendComple ted 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 3891
Don't worry about it, I restructured the code to use exceptions instead.

Steve
"Steve Barnett" <no****@nodomai n.comwrote in message
news:uD******** ******@TK2MSFTN GP03.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.addre ss", "My Name");
private SmtpClient smtp = new SmtpClient("my. isp.smtp.addres s");
private String token;

public Emails()
{
smtp.UseDefault Credentials = true;
// Hook in to the send completed event. We want to know when
this finishes
smtp.SendComple ted += new
SendCompletedEv entHandler(smtp _SendCompleted) ;
}
public void SendEmail(strin g emailTitle, string emailText, string
recipient)
{
MailMessage msg = new MailMessage(fro mAddress, new
MailAddress(rec ipient));
msg.IsBodyHtml = true;
msg.Body = emailText;
msg.BodyEncodin g = System.Text.Enc oding.UTF8;

msg.Subject = emailTitle;
msg.SubjectEnco ding = System.Text.Enc oding.UTF8;

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

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

void smtp_SendComple ted(object sender,
System.Componen tModel.AsyncCom pletedEventArgs 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.ToSt ring());
}
else
{
resultsText = String.Format("[{0}] Message sent.",
token);
}

MailResults(res ultsText);
}
}
}
}

I've put a breal point in the smtp_SendComple ted 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
7682
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 the app, even the first time. The application will be required to be sending out repeated emails, about one every second or two. Must this be...
6
10481
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 we.config, and it works. However, I would like to place it in my web.config file, and then just get the value from the web.config? Can someone...
6
2511
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, I'm having trouble writing to the server's event log. Here's some details: Server and workstation both in the same workgroup Logged into server...
10
2983
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 for their version in the book to the best of my abiltiies, and came across issues, mostly that a type was expected when trying to compile in MS...
1
1390
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 sending mail ****************/ // All the mail settings are extracted from the web.config file. string from =...
4
6200
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 Struts: import java.io.File; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator;
6
5476
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 it sometimes works too, but often does not. If it does not, he will never - until the next reboot. Then the "game" starts again. I get the exception...
7
22852
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 outlook or outlook express it works fine.. System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage("myemail@mydomain.com",...
2
3826
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 hosting company can't find what's wrong with it so I'm here hoping somebody can help. The hosting company tried sending email from the server and it was...
0
7471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7830
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6071
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1962
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 we have to send another system
1
1082
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
784
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.