Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Send mail through SMTP server

Send mail through SMTP server

Scheduled Pinned Locked Moved C#
csharpwinformssysadminquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    loyal ginger
    wrote on last edited by
    #1

    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!

    A OriginalGriffO 2 Replies Last reply
    0
    • L loyal ginger

      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!

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • L loyal ginger

        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!

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          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.

          L Offline
          L Offline
          loyal ginger
          wrote on last edited by
          #4

          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!

          OriginalGriffO 1 Reply Last reply
          0
          • L loyal ginger

            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!

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            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.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            L 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              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.

              L Offline
              L Offline
              loyal ginger
              wrote on last edited by
              #6

              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!

              OriginalGriffO 1 Reply Last reply
              0
              • A Abhinav S

                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

                L Offline
                L Offline
                loyal ginger
                wrote on last edited by
                #7

                I have verified that the antivirus software running on my machine was the problem. Your help is appreciated.

                1 Reply Last reply
                0
                • L loyal ginger

                  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!

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  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 no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  L 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    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.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    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).


                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      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.

                      L Offline
                      L Offline
                      loyal ginger
                      wrote on last edited by
                      #10

                      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.

                      L 1 Reply Last reply
                      0
                      • L loyal ginger

                        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.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        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).


                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups