0

I am writing a program in C# using Windows Forms and I am stuck at this part.

When any user logs in to the program for the first time in any given week, an email should be sent to all users who have a task (the task will be assigned by an admin). If a user has no tasks, he/she should not receive an email. When the second user logs in for that week, emails should not be sent.

I mean when the any first user of the program logging in, the emails will be send for all users who has tasks (to remind them to do the task). The problem is I do not want one user to receive too many duplicate emails.I already have the code for sending the emails, but I need a way to handle the rest of the process. I have researched and I saw that Windows Services might be an option.

Is there another way to do this?

public int OutLook_Send_Email_To_User(string user_Email, string email_Subject, string email_Content)
{
    try
    {                
        Outlook.Application outApp = new Outlook.Application();
        Outlook.MailItem outMsg = (Outlook.MailItem)outApp.CreateItem(Outlook.OlItemType.olMailItem);
        Outlook.Recipient outTo = null;

        outApp = new Outlook.Application();
        outMsg = (Outlook.MailItem)outApp.CreateItem(Outlook.OlItemType.olMailItem);

        outTo = (Outlook.Recipient)outMsg.Recipients.Add(user_Email);
        outTo.Type = (int)Outlook.OlMailRecipientType.olTo;
        outTo.Resolve();

        outMsg.Subject = email_Subject;
        outMsg.HTMLBody = email_Content;

        outMsg.Save();
        outMsg.Send();

        outTo = null;
        outMsg = null;
        outApp = null;

        return 0;
    }
    catch (Exception ex)
    {
        return -1;
    }
}
ivayle
  • 1,070
  • 10
  • 17
Serenade
  • 50
  • 1
  • 11
  • 1
    Without more information about how you're handling application logins and tasks, it's going to be difficult to answer this question. –  Mar 01 '17 at 02:08
  • @arbitrarystringofletters there will be a long story if I show all this – Serenade Mar 01 '17 at 02:10
  • Can you summarize the process then? –  Mar 01 '17 at 02:10
  • "And the email will be send once a week when the FIRST user logging in, that means when the SECOND user logging in, no email send.", this sentence makes little sense to me. What do you mean by FIRST and SECOND? Are these two completely users you're talking about? Are you saying that the TASK can be assigned to multiple users, but the email is sent only to the FIRST user? – Adrian Sanguineti Mar 01 '17 at 02:14
  • @Adrian I mean when the any First user of the program logging in, the emails will be sent for all users who has tasks ( to remind them to do the task). The purpose is I don't want 1 user received too many duplicate email. – Serenade Mar 01 '17 at 02:27
  • @arbitrarystringofletters yeah yeah let me summarize the process, feel free to ask if you dont understand. User login => program automatic send emails to all users have tasks. the email is the remind them to do the tasks. – Serenade Mar 01 '17 at 02:29
  • OK cool. Can you [edit](http://stackoverflow.com/posts/42521790/edit) your question and reworded so that it is clearer. – Adrian Sanguineti Mar 01 '17 at 02:36
  • already edit, thank you @Adrian – Serenade Mar 01 '17 at 02:48
  • Have you already designed the log in and task components? –  Mar 01 '17 at 02:51
  • Tie it to a database and keep track of 1) tasks, 2) logins, and 3) whether the email has already been sent. – smoore4 Mar 01 '17 at 02:53
  • @arbitrarystringofletters all set, now just the email part :) – Serenade Mar 01 '17 at 02:56
  • @SQLDBA can you be more specific? – Serenade Mar 01 '17 at 02:56
  • Try something basic first. Set up a table with login, email_sent (default 0), and email_date. When a login happens, run some code that looks similar to this: string m_str_update = "UPDATE my_table SET email_sent = '1, email_date = GETDATE(), login = " + m_login; SqlCommand m_cmd_update = new SqlCommand(m_str_update, m_conn_data); m_cmd_update.CommandType = CommandType.Text; m_cmd_update.ExecuteNonQuery(); When you can get to that point, and you post your code, I'll give you more detail. – smoore4 Mar 01 '17 at 03:05
  • @SQLDBA I understand what you said, this table is created meaning when the email_sent = 1, the program scan the table and stop sending email right? – Serenade Mar 01 '17 at 03:10
  • Yes. that is right. Except it will end up being more complicated. Just try something simple first. – smoore4 Mar 01 '17 at 03:13
  • @SQLDBA my idea is not what you mean? – Serenade Mar 01 '17 at 03:47
  • @SQLDBA hey it's me, again, how can I communicate with you? I have some questions to ask... – Serenade Mar 06 '17 at 06:32

0 Answers0