Is there a way for me, with a WPF browser application, to schedule an email to be sent? What I will have is a date, date of user subscription, and then I would like it to send the user an email 5 days before their membership expires. How can I achieve this?
The easiest way to do this is to write a record to a database table (say a email task table) then write a Windows service that polls this table say every minute, checks for any emails that need to be sent and then sends them.
Seems to be you'll have to do something on the server side with a scheduled job/task checking daily for who needs to get an email sent to them.
AFAIK the SMTP server sends the emails immediately so you have to persist this queue in your application (or use MSMQ as queue storage) and have some part of your application executing the delivery at required time.
At this point my question is, if you can send when you know is the time to send, can't you send directly later without using the queue or do you have any logic in your application so that the email you would generate today can't be generated at later time?
I would probably create a Windows Service or, even better, have a scheduled task calling my application every day at midnight then the application verifies to whom the email should be sent right now, creates it and sends it out.
Related
I'm making an MVC app with the .NET Framework and in one of my controllers I call an async task that sends an e-mail to the signed in user.
This task is called upon when the user clicks a specific checkbox and the e-mail is meant to work as sort of reminder.
The entire task works as intended (the user gets an e-mail when the checkbox is checked), but I need it to wait 24 hours before actually sending the e-mail, as it is a reminder.
Currently the e-mail is sent right away, how can I delay the completion of my "e-mail task", while the code continues?
Use a library like Hangfire which lets you schedule background jobs and backs them with persistent storage.
You can then easily schedule a job like:
BackgroundJob.Schedule(
() => SendEmail("user#domain"),
TimeSpan.FromDays(1));
This is a classic X Y Problem. While it may be possible to make your system wait 24 hours you are creating a very fragile system that can be affected by app pool resets and server reboots.
Putting aside the possibility of an unexpected reboot, what happens when your maintenance cycle comes around and a scheduled reboot is going to happen? How many queued email reminders will you have that you can't do anything with?
The best approach for systems that don't immediately use their data is to buffer it through some form of storage scheme. It could be as simple as writing queued emails to files on the system, or something more robust like a database with a dedicated email sending service.
I have used a LOT of email sending systems over the years, and even for immediate sends we have used a database intermediary, with one dedicated email sending Windows service to produce and send the actual email. By centralizing the email production you not only get one place to maintain your email sending code, but you can also increase the durability of the whole system.
Bonus points if your database is part of a high availability cluster, as in this kind of system the database becomes the critical point. If it is then you're protected from any form of downtime other than a total network outage.
Let the Task wait for 24 hours before sending the mail.
await Task.delay(TimeSpan.FromHours(24));
Add this line in your async function bfore sending the email
I have an email parser application in C# that uses Exchange Web Service where it retrieves incoming emails from the exchange account. Is it possible to have it run real time in the background? Whenever an email arrives, it automatically does its job without me having to click it every time or using a task scheduler. The first plan is task scheduler which I set to run every 5 minutes but I need a more efficient way as this app will receive heaps of emails which will then send a reply back to the sender.
Let me know if I missed some information or if my question is too vague.
Edit:
I did what L-Three suggested but... (please read my reply to BugFinder). The program must only trigger if there is an incoming email. Looping is consuming too much of CPU usage. Is it possible to do this?
I think you are looking for EWS Push Notifications (see https://msdn.microsoft.com/en-us/library/office/dn458791(v=exchg.150).aspx)
If you google for this term, you will find some sample programs, eg http://www.codeproject.com/Articles/73834/EWS-Mail-Notifier.
I have an sql database where I store an email message, subject, recipients and the data time when this message has be to be emailed. How can I do it programmaticaly via console app lets say that I hook as an executable via task scheduler?
What should I use - Thread Timer in order to determine when to fire off the email?
Never done this before really hoping that someone can help.
I've tried Quartz but can't figure it out.
You'll need to know how to connect to a database, create tables, query the database, send emails, your question is too general to answer it in one post but the below is an overview.
Use a database to hold the email. Make one of the columns in the database equal to the time when you want the message sent. Have the application query the database a few times a minute and if the datetime of the TimeToSend is greater than or equal to Datetime.Now() send the email. Once the email is sent either delete it or move it to a SentEmails database table. This would allow another separate program(s) to drop new emails into the database to be sent at a future time.
You could add more columns to specify things like how often the email should be sent or other conditions that you can evaluate through code to add whatever flexibility is required.
I have a web application from which emails should be sent after specific actions. I have some alternatives for handling this I'm not sure which one is the best.
The first is that, when a user does an action the email is being sent directly from the ASP.NET application. But I think this is not a really reliable system because if the SMTP server is down or something else happens, the user just gets a feedback that his action cannot be completed.
As an alternative I was thinking about implementing a queuing system for what I have some ideas:
Push emails to send, into a database table, and a service application periodically checks for new messages, then sends them. On successful send it marks the email task completed.
Use MSMQ for queing. In this case the whole email could be passed as a message; or the other way is to store the message with attachments into a db table, and pass only the data which is required to query the db table and send the message. In this case I don't have to deal with size limits of MSMQ (because of attachments).
something else, like a local WCF service to notify the service
Which way you think is the best?
Use MSMQ is not good solution since has a limitation of 4 MB of each size. http://blogs.msdn.com/b/johnbreakwell/archive/2007/08/22/why-is-there-a-4mb-limit-on-msmq-messages.aspx
Worse case scenario, if MSMQ is failed like it process throw error or suddenly shutdown, it will loss many message. In my case, this solution is good when hardware and software is instaled in almost ideal
Use database and window service is better since it is a simple and doesn't need much effort.
I usually use a combination of database and file. The database contains table to save a header information and a flag that message has been action (either success or error or else) and files contains message (either html or plain) and attachment in original format.
When process is run to send, it is quicker to assemble a message from files rather than from querying blob/clob.
Since they are using file system on the application, you can add hardware like server or components or else to add availibility of the system easily.
Database can be added too, but it will cost you more license in databse software.
I add a test send email after send email in x times to make sure it is works well; this test email is send to my self or dummy inbox and an application to check the test email that is the same email that send and receive. If it is the same, sending pending email will continue again
Another way if you are using MS Exchange, you can use message queue by utilize its web service to queue send. This is an easy way but you need license.
You can see on MSDN library how to utilize MS Exchange web service.
You can use an email server like hmail. In order to push emails into a queue, you can push them to a mail server. To do that, you can write a windows form application that has a timer object that checks every row that has a Status 0(not sent) in email table. When the thread sends it to the mail server, it will be marked as 1(sent).
You can also classify your emails if you use DB. Different actions can send different emails. You can store this info in DB also so that your windows form application thread will now which email template to send.
We are writing a feature to send a reminder email to customers in x number of days and just wondered if it was possible to delay the sending of the emails similar to how you can in Outlook (New Mail > Options button > Do not deliver before) in C#.
Does anyone know of a way of doing this?
If you want to submit the email to an SMTP server to hold for a number of days, the server would have to support this feature. I would recommend against doing it that way, however. If you had a problem with missing emails it could be hard to track down.
Here's what I would do:
Schedule the emails in your system by making a table entry.
Include a "SendDate" column in the table where you can set a date in the future
Write a Windows service that wakes up once every 15 minutes (for example) and sends the emails in that table where SendDate < currentDate
One possibility is to create a service that runs on a scheduled task processing 'pending' mail. You might store the pending mail in a SQL Database. (Dave details this)
This article looks promissing if you would like to avoid a 'real' service
http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
No.
The way to accomplish this would be for your application to write the email to a queue (could be database, file, MSMQ, etc), and then write a separate process that, at a later date, reads mail from that queue and sends it.
As far as I know, the SMTP protocol doesn't have any kind of "delayed send" feature. Also, the feature you reference in Outlook actually just keeps it in your client's outbox until the designated date, so it's a client-side feature, not a server-side feature.
The SmtpClient is a pretty dumb client and doesn't support this -- sends are immediate whether they are synchronous or asynchronous. You may be able to do it using an Exchange integration. I'm not sure what APIs are exposed for Exchange, though. The simplest way would be to keep the message in a database table along with the time-to-send and have an off-line process that regularly scans this table and looks for messages whose time-to-send value has passed and are not marked as sent.