-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerThreadDerived.cs
125 lines (109 loc) · 4.86 KB
/
WorkerThreadDerived.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Net.Mail;
namespace MSMQListner
{
internal class WorkerThreadDerived : WorkerThread
{
// calls the base class constructor
public WorkerThreadDerived(WorkerInstance workerInstance, WorkerThreadFormatter workerThreadFormatter)
: base(workerInstance, workerThreadFormatter) { }
// when starting obtain a reference to the assembly and construct an object
override protected void OnStart()
{
}
// when stopping release the resources
override protected void OnStop()
{
}
// override the pause and continue methods
override protected void OnPause()
{
}
override protected void OnContinue()
{
}
// method to perform the processing of the message
override protected void ProcessMessage()
{
MailMessage oMail = null;
EmailMessage iMail = null;
SmtpClient s = null;
// attempt to call the required Process method
try
{
//// define the parameters for the method call
iMail = (EmailMessage)inputMessage.Body;
//if (iMail != null && iMail.attempt < 3)
{
int current = 0;
if (messageCount.ContainsKey(inputMessage.Label))
{
current = messageCount[inputMessage.Label];
messageCount[inputMessage.Label] = current + 1;
}
else
{
messageCount.Add(inputMessage.Label, 1);
}
MailAddress to = new MailAddress(iMail.to);
MailAddress from = new MailAddress(iMail.from, iMail.displayName);
string body = iMail.body;
string subject = iMail.sub;
oMail = new MailMessage(from, to);
if (oMail != null)
{
oMail.Subject = subject;
oMail.Body = body;
oMail.IsBodyHtml = true;
oMail.Priority = MailPriority.Normal;
//oMail.Sender = from;
//oMail.Headers.Add("X-VirtualServerGroup", "mail1.domain.com");
//oMail.Headers.Add("DomainKey-Signature", domainKey);
//oMail.Headers.Add("DKIM-Signature", DKIM);
s = new SmtpClient(smtpServer);
// s.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
s.ServicePoint.MaxIdleTime = 2;
s.ServicePoint.ConnectionLeaseTimeout = 0;
if (s != null)
{
s.Send(oMail);
}
}
oMail.Dispose();
}
iMail = null;
}
catch (InvalidCastException ex)
{
string er = "Unable to Process message - " + inputMessage.Label + ";Email - " + iMail.to + "; Current Attempt - " + iMail.attempt + "; Current Error Count - " + messageCountAtError + "; Error - " + ex.Message + ";Inner Ex - ";
if (ex.InnerException != null)
{
er = er + ex.InnerException;
}
// if an error in casting message details force a non critical error
throw new WorkerThreadException(er, false);
}
catch (SmtpException ex)
{
// if an error calling the assembly termiate the thread processing
string er = "Unable to Process message - " + inputMessage.Label + ";Email - " + iMail.to + "; Current Attempt - " + iMail.attempt + "; Current Error Count - " + messageCountAtError + "; Error - " + ex.Message + ";Status Code:" + ex.StatusCode;
if (ex.InnerException != null)
{
er = er + ";Inner Ex - " + ex.InnerException;
}
throw new WorkerThreadException(er, false);
}
catch (Exception ex)
{
// if an error calling the assembly termiate the thread processing
string er = "Unable to Process message - " + inputMessage.Label + ";Email - " + iMail.to + "; Current Attempt - " + iMail.attempt + "; Current Error Count - " + messageCountAtError + "; Error - " + ex.Message + ";Inner Ex - ";
if (ex.InnerException != null)
{
er = er + ex.InnerException;
}
throw new WorkerThreadException(er, false);
}
// if no error review the return status of the object call
}
}
}