Author |
|
mlts Newbie
Joined: 12 November 2014
Online Status: Offline Posts: 15
|
Posted: 28 November 2014 at 2:16am | IP Logged
|
|
|
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 |
|
|
mlts Newbie
Joined: 12 November 2014
Online Status: Offline Posts: 15
|
Posted: 28 November 2014 at 2:22am | IP Logged
|
|
|
There's a
server.Message = msg;
in ServerSend too. missed that and cant find edit option to edit my post.
|
Back to Top |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 28 November 2014 at 2:54am | IP Logged
|
|
|
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 |
|
|
mlts Newbie
Joined: 12 November 2014
Online Status: Offline Posts: 15
|
Posted: 28 November 2014 at 3:54am | IP Logged
|
|
|
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 |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 28 November 2014 at 4:01am | IP Logged
|
|
|
Please enable logging into a file (with Smtp.Log property) to see at which stage it's aborting.
Regards,
Alex
|
Back to Top |
|
|
mlts Newbie
Joined: 12 November 2014
Online Status: Offline Posts: 15
|
Posted: 28 November 2014 at 4:01am | IP Logged
|
|
|
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 |
|
|
mlts Newbie
Joined: 12 November 2014
Online Status: Offline Posts: 15
|
Posted: 03 December 2014 at 2:23am | IP Logged
|
|
|
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 |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 03 December 2014 at 2:47am | IP Logged
|
|
|
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 |
|
|
mlts Newbie
Joined: 12 November 2014
Online Status: Offline Posts: 15
|
Posted: 03 December 2014 at 6:37am | IP Logged
|
|
|
Ok, works fine now. thanks.
Great support btw :)
|
Back to Top |
|
|