I want to ask about dealing with bounce-backs emails (but on server without IMAP, only with POP3, and without an Outlook on my machine).
I don’t want to read all emails from server every time.
So I thought, At first:
1. read e-mails from the oldest to the newest, checking whether there are bounce-backs.
2. save the date of the latest e-mail to the variable "TEMPDATA"
//This process will be executed only once, at the beginning
And then in timer loop (in another thread):
3. read e-mails from the "TEMPDATA" to the newest, checking whether there are bounce-backs (check if there is a subject “"Undelivered Mail Returned to Sender"”.)
The pseudo-code of my idea:
TIMER( Tick every 5minutes )
{
(LOCK)
loop(on inbox from the "TEMPDATA" to the newest)
{
// Read single email
if (the subject is "Undelivered Mail Returned to Sender")
{
// Here I will parse message so that I will know the addresse
// And removie that addresse from my list.
}
}
(UNLOCK)
}
I’m just curious if there is some better solution.
If you are planning to use IMAP protocol I'd rather use email UID than date.
Checking only the subject is not the bast way to check, if the message is delivery failure report.
Some emails will have different subject (different language).
Gmail for example uses additional header.
Most delivery failure emails however follow RFC3464 specification and use Delivery Status Notification (DSN) format.
You can try using Mail.dll component for this:
http://www.lesnikowski.com/blog/index.php/bounce-handling/
Please note that this is a commercial product I created.
I know this is an old question, but it shows up high in search results, so here's to hoping it helps others.
I found this open source solution:
https://mailsystem.codeplex.com/
Seems to be working well for us so far. Documentation is pretty much non-existent, though the Visual Studio intelli-sense is pretty detailed.
Related
Is there an elegant automatic way how not to send MailMessage emails without commenting them out? I don't wan't to modify my code every time I am testing something in pages thats uses MailMessages.
a method i have used in the past is to have the configuration for your SMTP server in your web.config or app.settings.json and when you are performing local development point it towards a local instance of https://github.com/mailhog/MailHog or equivalent. This will catch all the emails you send without every having to worry about actually letting them out into the world
Your code would remain unchanged which is the best part.
If my .NET app sends out one email followed by another, sometimes the 2nd arrives 1st.
Is there a way to control the order they get sent.
Not really, email delivery is a function of the server. The best you can do is to delay the second email by some arbitrary amount of time, but even that is no guarantee. You could maybe use a scheduling component to facilitate that. Why does the order matter? Maybe knowing that will help us find an answer for you.
I'm looking to build an HL7 listener in C#. We are already parsing messages that are sent to us as text files and importing them into the database, so I have an understanding of what HL7 messages are and how to parse them.
My main question regarding the listener. Is the listener simply a TCP listener? If so, could I put together a basic TCP listener that gets the message and parses the results, importing them into our database?
My second questions is regarding the ACK. My understanding of this is it's simply a message sent back to the sender after receiving a message. It's simply another HL7 message designated as a response message.
Am I correct in my understanding? Also if anyone has any additional info or pointers I would appreciate any help.
Yes it's just a simple TCP listener.
To acknowledge a message you have to return an MSH message which should look somehting like this:
<11> this means a byte represented in a decimal value. this is VT from the ascii table.
<11>MSH|^~\&|KS||LAB||20040915080800||ACK|59793000678|P|2.2|59793000678<13>
MSA|AA|59793000678<13>
<28><13>
You should probably look at: http://nhapi.sourceforge.net/home.php
Several items:
The "protocol" run over the socket is the HL7 Minimal Lower-layer Protocol (MLP or sometimes MLLP). This simple protocol wraps HL7 messages with start and end characters. A description HL7 MLP wrapping characters.
As noted by #the_ajp, as a receiver of an HL7 message, you need to build an HL7 Acknowledgment. There are some archaic rules involved but ultimately the part that is key is that you Application Accept ("AA") the message in MSA-1. Some senders are very picky about their Acks and require unique message IDs, acknowledgment of correct message, correct trigger code in MSH-9, etc. Details on the HL7 ACK Message; discussion of original and enhanced HL7 acknowledgments.
Clearly you can grow your own HL7 subsystem that is hard coded. Prior to doing that, however, consider the total costs of building and supporting an HL7 subsystem, look at some free options like Mirth, and consider commercial solutions like Corepoint or Interfaceware.
Disclosure: I'm co-chair of the HL7 Infrastructure and Message (InM) committee, CTO at Corepoint, and Chief Architect of the Cloverleaf integration engine.
Buy Vs. Build. If you've already "done" your HL7 interface and it works off of the file-system then why on earth would you want to rebuild something that's so readily available. There's the Mirth project which is open source or if you want something that
a) Does exactly what you're looking for out of the box (in that it was designed for exactly the scenario you outline).
b) Has one of the (if not THE) lowest cost in the world for commercial HL7 software of this kind.
c) Installs and configures in minutes.
Investigate a little bit and look at this: http://www.hermetechnz.com/EasyHL7/Prod_Listeners.asp.
Unless you are really looking at doing many hundreds or thousands of deployments a year you just have to ask yourself if it's the best use of your time.
DISCLOSURE: I work in customer support at this company, but we don't employ salespeople at all, it's just a great, cheap, reliable HL7 product in use all over the world.
I have a site that people put stuff for sale on.
Every month, every user who has something for sale will get sent an email by a windows service asking them if their item has been sold and giving them a custom link to click to confirm it hasn't (as it's against the user agreement for items to remain on the site that are sold).
First I must run a query to get all the unsold items and with the related users email.
Currently I am looping all these and generate a custom email for each person and sending them out as individual emails.
foreach (Item unsoldItem in unsoldItemsCollection)
{
//generate email
string email = GenerateUnsoldEmail(itemName, itemPrice);
Utils.Sendemail(unsoldItem .UserEmail, "no-reply#website.com", "Unsold Item", email);
}
(this is kind of pseudo code, but this is pretty much what I'm doing)
My problem is there could possibly be thousands ( if it takes off, maybe millions :) ) of items in that loop all requiring emails, which I think is going to cause problems.
What other way could I do this?
Bex
I would do this asynchronously so that the program is not halted until the current email is sent.
You can't mail merge since all emails are different; links inside it will be different... unless the body of the email has some sort of landing page that forces the user to log in and then redirects the user to a specific page where all the items for the user are listed and he is asked to remove the ones already sold. In that case, you could send an email instead of thousands.
SmtpClient has an async version of the Send method
If you want to do something for every item in a collection, you will have to use some kind of loop (or something equivalent, like recursion). There is no way around that.
You should stop worrying about potential problems. If you think this code will be a performance problem, measure it and find out how fast it actually is and how many emails it will handle.
You're worried it might take hours to send all the emails. Is that actually a problem? Why?
You're also worried it might take too much memory. If you can't fit all items into memory at once, you should process them in batches: get for example 1000 at a time, process them, and then get another 1000.
You can create a windows service which will send all your emails once a day.
Or, even simpler, you can create a small console utility which does the same and run it as a scheduled task.
After experimenting with many IMAP API's, I have decided to write my own (Most are memory hogs, some just do not work, out of memory exception etc etc etc).
Anyway I have wrote some code that works (using TCPClient object) and its good so far. However, I want my one to be able to handle multiple requests to the mail server. For example:
Say I get a list of all the UIDs, then I cycle through this list getting what I want (Body, Header, etc) for each message.
The question is, how would I handle, multiple requests at the same time? So instead of looping through the UIDs one at a time I can instead process 10 at a time.
What would be the best approach here? An array of TCP Clients, each with its own thread?
Thanks.
In general it's recommended that IMAP clients only have at most one connection to the server at all times. Not only does additional connections require valuable resources on the server but more important the IMAP specification does not guarantee that two connections can select the same mailbox at the same time. Relying on this capability being present on the server may render your client incompatible with those servers.
Instead you should use the protocol as efficient as possible. Note that many commands can operate on a set or range of UIDs. This allows you to make one singe request where you specify every UID instead of making one request for each UID separately.
Another good practice is to not request more data than what is currently needed. For example say that you have a list of messages. Then don't request detailed information for all of them, only request information for the messages that are currently visible.
I highly recommend that you read RFC 2683, IMAP4 Implementation Recommendations. It covers this among other things.
If you decide to use multiple connections anyway then a good approach is usually to use asynchronous operations and not use individual threads explicitly. Combination with some kind of run loop integration is often useful as well, that way your code is called when there is data to read instead of you code having to poll or explicitly check for it. This is often a good approach even if you're only using a single connection. Keep in mind that according to the IMAP protocol the server may send you responses even when you have not explicitly asked for them.