Search The ForumSearch   RegisterRegister  LoginLogin

MailBee.NET SMTP

 AfterLogic Forum : MailBee.NET SMTP
Subject Topic: Processing is aborted by user Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
mlts
Newbie
Newbie


Joined: 12 November 2014
Online Status: Offline
Posts: 15
Posted: 28 November 2014 at 2:16am | IP Logged Quote mlts

Hi.
We've been using SMTP with success so far. However, a recent change in our code made it no longer work. Here is some pseudo code for understanding the situation:

Smtp server = new Smtp();
server.DnsServers.Add(DNS1);
server.DnsServers.Add(DNS2);
server.DirectSendDefaults.HelloDomain = Domain;
server.DirectSendDefaults.LocalEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(IP), 0);

List<OurMailClass> mailsToSend = GetMails();
foreach(OurMailClass mail in mailsToSend)
{
    MailMessage msg = CreateMail(mail);
    ServerSend(server, msg);
}

private void ServerSend(SMTP server, MailMessage msg)
{
    server.Send(envelopFrom, (string)null);
}

The above construct works ok.
However, when we make the sending run in a seperate thread, like this:

foreach(OurMailClass mail in mailsToSend)
{
    MailMessage msg = CreateMail(mail);
    Thread t = new Thread(() => ServerSend(server, msg));
    t.Start();
}

The sending function crashes with exception: "Processing is aborted by user."
My question is: is this an incorrect use of the SMTP class?
In an article: http://www.afterlogic.com/support/tutorials-mailbee-net/21-asynchronous-methods-of-mailbee-net.asp
you mention about sendBegin, but i understood that as being optional, and that we can do it in our own worker threads. Is it not?
Back to Top View mlts's Profile Search for other posts by mlts
 
mlts
Newbie
Newbie


Joined: 12 November 2014
Online Status: Offline
Posts: 15
Posted: 28 November 2014 at 2:22am | IP Logged Quote mlts

There's a
server.Message = msg;
in ServerSend too. missed that and cant find edit option to edit my post.
Back to Top View mlts's Profile Search for other posts by mlts
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 28 November 2014 at 2:54am | IP Logged Quote Alex

The code seems to be correct. The error you mentioned occurs when Smtp.Abort method is called.

Maybe, when the code was run on the main thread, the app just had nothing else to do but run the mail sending code. Now you're running on a worker thread, and the main thread is free for other work, such as the ability to exit the application. If the main thread completes before sending on the worker thread completed, this may end up in aborting the whole application.

Are you sure the main thread does not finish too early?

Also, are there any means of calling .Abort in the code?

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 
mlts
Newbie
Newbie


Joined: 12 November 2014
Online Status: Offline
Posts: 15
Posted: 28 November 2014 at 3:54am | IP Logged Quote mlts

Thank you for your quick answer.
There is no Abort at all anywhere in our code.
And I'm sure the main thread does not exit. It's a service running constantly.
I made even one more check, I added while(true) Thread.Sleep(1000) right after the foreach loop, to make sure that everything stays open and i placed a debug point in visual studio there, next i run the application and the while loop was going infinetly, without letting the application or even the function to exit, but i still got the same error.
I also removed the Thread t (...) t.start(); to run in single thread - and this time there was no error, message was sent properly.
Back to Top View mlts's Profile Search for other posts by mlts
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 28 November 2014 at 4:01am | IP Logged Quote Alex

Please enable logging into a file (with Smtp.Log property) to see at which stage it's aborting.

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 
mlts
Newbie
Newbie


Joined: 12 November 2014
Online Status: Offline
Posts: 15
Posted: 28 November 2014 at 4:01am | IP Logged Quote mlts

Oh,
Sorry. I found the reason. Everything works ok.
There was no abort, but there was server.Dispose(); called after the sending function, it was called before the sending finished and i missed that.
So yeah, now everything works fine,
thank you for your assistance.
Back to Top View mlts's Profile Search for other posts by mlts
 
mlts
Newbie
Newbie


Joined: 12 November 2014
Online Status: Offline
Posts: 15
Posted: 03 December 2014 at 2:23am | IP Logged Quote mlts

Update,
Althought the sending works, if we have more than one message sent in quick successions (most probably first message does not finish sending before the second one starts) we get the following error:
Message:MailBee.MailBeeInvalidStateException: There is already an operation in progress.
Can one SMTP Server send only one message at the time ?
Our aim was to create a parallel sending to multiple servers - each in new thread. Does this mean i have to create new SMTP object for each such send?
Back to Top View mlts's Profile Search for other posts by mlts
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 03 December 2014 at 2:47am | IP Logged Quote Alex

If you spawn multiple threads, each one must use its own Smtp instance. If you want to send multiple messages with a single instance simultaneously (so that MailBee.NET will organize its own multi-threading), use .AddJobs and .SendJobs for that. This lets you form up a queue of emails to be sent out.

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 
mlts
Newbie
Newbie


Joined: 12 November 2014
Online Status: Offline
Posts: 15
Posted: 03 December 2014 at 6:37am | IP Logged Quote mlts

Ok, works fine now. thanks.
Great support btw :)
Back to Top View mlts's Profile Search for other posts by mlts
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide