Send mail through SMTP server
-
I am playing with a small program that sends emails through SMTP server. This is a C# Windows Forms application. The main part of the program looks like this:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;namespace test_email_smtp
{
public partial class send_mail_through_smtp : Form
{
public send_mail_through_smtp()
{
InitializeComponent();
}private void send\_mail\_Click(object sender, EventArgs e) { SmtpClient client = new SmtpClient(smtp\_server.Text); MailMessage message = new MailMessage(send\_from.Text, send\_to.Text); message.Body = mail\_body.Text; message.Subject = mail\_subject.Text; client.Send(message); message.Dispose(); } }
}
"send_from", "send_to", "mail_body", "mail_subject", and "smtp_server" are TextBox controls on the Form. When I ran the program and click the "send" button, and then close the program (by closing the form), the mail is sent IMMEDIATELY. However, if I click the "send" button and leave the program running (not closing the program), after a minute or so, I will get a message saying "Your mail message was unable to be sent because the connection to your mail server was interrupted. Please open your email client and re-send the message from the Sent Messages folder." This does not happen all the time. I'd say 9 out of 10 times this message was shown. The one time it was not shown, the mail was sent after a long time. By the way, the message is from "Symantec Email Proxy". When this message was shown, a balloon message was also popped up by the system tray area saying "Scanning message 1 of 1", also from Symantec. All these problems do not exist if I close the program after I click the "send" button. The mail will be sent immediately. I need the program to keep running after the mail is sent. I can't close the program every time a mail needs to be sent. Any ideas? Thanks!
-
I am playing with a small program that sends emails through SMTP server. This is a C# Windows Forms application. The main part of the program looks like this:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;namespace test_email_smtp
{
public partial class send_mail_through_smtp : Form
{
public send_mail_through_smtp()
{
InitializeComponent();
}private void send\_mail\_Click(object sender, EventArgs e) { SmtpClient client = new SmtpClient(smtp\_server.Text); MailMessage message = new MailMessage(send\_from.Text, send\_to.Text); message.Body = mail\_body.Text; message.Subject = mail\_subject.Text; client.Send(message); message.Dispose(); } }
}
"send_from", "send_to", "mail_body", "mail_subject", and "smtp_server" are TextBox controls on the Form. When I ran the program and click the "send" button, and then close the program (by closing the form), the mail is sent IMMEDIATELY. However, if I click the "send" button and leave the program running (not closing the program), after a minute or so, I will get a message saying "Your mail message was unable to be sent because the connection to your mail server was interrupted. Please open your email client and re-send the message from the Sent Messages folder." This does not happen all the time. I'd say 9 out of 10 times this message was shown. The one time it was not shown, the mail was sent after a long time. By the way, the message is from "Symantec Email Proxy". When this message was shown, a balloon message was also popped up by the system tray area saying "Scanning message 1 of 1", also from Symantec. All these problems do not exist if I close the program after I click the "send" button. The mail will be sent immediately. I need the program to keep running after the mail is sent. I can't close the program every time a mail needs to be sent. Any ideas? Thanks!
-
I am playing with a small program that sends emails through SMTP server. This is a C# Windows Forms application. The main part of the program looks like this:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;namespace test_email_smtp
{
public partial class send_mail_through_smtp : Form
{
public send_mail_through_smtp()
{
InitializeComponent();
}private void send\_mail\_Click(object sender, EventArgs e) { SmtpClient client = new SmtpClient(smtp\_server.Text); MailMessage message = new MailMessage(send\_from.Text, send\_to.Text); message.Body = mail\_body.Text; message.Subject = mail\_subject.Text; client.Send(message); message.Dispose(); } }
}
"send_from", "send_to", "mail_body", "mail_subject", and "smtp_server" are TextBox controls on the Form. When I ran the program and click the "send" button, and then close the program (by closing the form), the mail is sent IMMEDIATELY. However, if I click the "send" button and leave the program running (not closing the program), after a minute or so, I will get a message saying "Your mail message was unable to be sent because the connection to your mail server was interrupted. Please open your email client and re-send the message from the Sent Messages folder." This does not happen all the time. I'd say 9 out of 10 times this message was shown. The one time it was not shown, the mail was sent after a long time. By the way, the message is from "Symantec Email Proxy". When this message was shown, a balloon message was also popped up by the system tray area saying "Scanning message 1 of 1", also from Symantec. All these problems do not exist if I close the program after I click the "send" button. The mail will be sent immediately. I need the program to keep running after the mail is sent. I can't close the program every time a mail needs to be sent. Any ideas? Thanks!
The suggestion made by Abhinav may help; if it does not, then: SmtpClient implements IDisposable so it may be that the email is sent when the client is disposed. This would give the symptoms you describe, at least in terms of the send-when-application-closed problem. Try wraping your section in a
using
block.using (SmtpClient client = new SmtpClient(smtp_server.Text))
{
MailMessage message = new MailMessage(send_from.Text, send_to.Text);
message.Body = mail_body.Text;
message.Subject = mail_subject.Text;
client.Send(message);
message.Dispose();
}Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
The suggestion made by Abhinav may help; if it does not, then: SmtpClient implements IDisposable so it may be that the email is sent when the client is disposed. This would give the symptoms you describe, at least in terms of the send-when-application-closed problem. Try wraping your section in a
using
block.using (SmtpClient client = new SmtpClient(smtp_server.Text))
{
MailMessage message = new MailMessage(send_from.Text, send_to.Text);
message.Body = mail_body.Text;
message.Subject = mail_subject.Text;
client.Send(message);
message.Dispose();
}Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
Thanks for your reply. I tried that and got the following compile time message:
Error 1 'System.Net.Mail.SmtpClient': type used in a using statement must be implicitly convertible to 'System.IDisposable'
I will do more research on this matter. Thanks!
-
Thanks for your reply. I tried that and got the following compile time message:
Error 1 'System.Net.Mail.SmtpClient': type used in a using statement must be implicitly convertible to 'System.IDisposable'
I will do more research on this matter. Thanks!
My apologies, it looks like MS are lying again... SmtpClient @ MSDN[^]
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
The suggestion made by Abhinav may help; if it does not, then: SmtpClient implements IDisposable so it may be that the email is sent when the client is disposed. This would give the symptoms you describe, at least in terms of the send-when-application-closed problem. Try wraping your section in a
using
block.using (SmtpClient client = new SmtpClient(smtp_server.Text))
{
MailMessage message = new MailMessage(send_from.Text, send_to.Text);
message.Body = mail_body.Text;
message.Subject = mail_subject.Text;
client.Send(message);
message.Dispose();
}Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
After disabling the antivirus program, the original program sends the email immediately when the button is clicked. That indicates that the antivirus was the problem. I still don't understand why closing the program bypasses the antivirus program's scanning of the email. Another issue is that it's hard to ask the program's users to turn off their antivirus program to facilitate the healthy running of my program. My solution to the problem may be to use separate processes for sending emails. I can spawn a small program just to send an email. It terminates by itself right after the mail is sent. Thank you for your time!
-
Trying disabling your antivirus to see if you get the same problem. If you don't, then it has something to do the the symantec firewall or virus scan.
My signature "sucks" today
I have verified that the antivirus software running on my machine was the problem. Your help is appreciated.
-
After disabling the antivirus program, the original program sends the email immediately when the button is clicked. That indicates that the antivirus was the problem. I still don't understand why closing the program bypasses the antivirus program's scanning of the email. Another issue is that it's hard to ask the program's users to turn off their antivirus program to facilitate the healthy running of my program. My solution to the problem may be to use separate processes for sending emails. I can spawn a small program just to send an email. It terminates by itself right after the mail is sent. Thank you for your time!
loyal ginger wrote:
Another issue is that it's hard to ask the program's users to turn off their antivirus program to facilitate the healthy running of my program.
I can see where that might be a problem. :laugh:
loyal ginger wrote:
My solution to the problem may be to use separate processes for sending emails. I can spawn a small program just to send an email.
X| But if you gotta, you gotta. What kind of virus scanner are we talking?
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
My apologies, it looks like MS are lying again... SmtpClient @ MSDN[^]
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
OriginalGriff wrote:
it looks like MS are lying again
Hmm. Continuously improving themselves, that is what they are doing. It took them till 4.0 to realize SmtpClient had to implement IDisposable. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
loyal ginger wrote:
Another issue is that it's hard to ask the program's users to turn off their antivirus program to facilitate the healthy running of my program.
I can see where that might be a problem. :laugh:
loyal ginger wrote:
My solution to the problem may be to use separate processes for sending emails. I can spawn a small program just to send an email.
X| But if you gotta, you gotta. What kind of virus scanner are we talking?
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
I have "Symantec AntiVirus" version 10.1.5.500 on my development machine. It's hard to predict what antivirus software will be on my client's machine. I am lucky to see this happen on my machine. Sometimes a fully tested application craps out on client's machines because some other software's interference, making the software developers look bad. I hope that does not happen to me.
-
I have "Symantec AntiVirus" version 10.1.5.500 on my development machine. It's hard to predict what antivirus software will be on my client's machine. I am lucky to see this happen on my machine. Sometimes a fully tested application craps out on client's machines because some other software's interference, making the software developers look bad. I hope that does not happen to me.
I would suggest you try .NET 4.0 and use the new SmtpClient.Dispose() method (directly or through a using statement, doesn't matter). I haven't done so myself yet, but it seems to be a new enhancement by MS, and I expect it to solve your problem. Of course, you should not disable or replace your AV. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).