How to make an email notifier like google calendar? - c#

I am using
asp.net mvc 2.0
C#
.NET 4.0
ms sql server 2005
iis 7.0
I want to make an email notifier system just like many sites have such as google. I want user to be able to set a reminder date and when that date is hit a email is sent to them.
I am not sure how to do this.
I heard of a couple ways but have not found any tutorials on how to do them.
Windows scheduler
through ms sql server (think sql server agent?)
With the windows scheduler I don't think it would work on a shared hosting environment. I would prefer if it did but if there is a big difference then I can live with out that ability.
I also want in the very near future to support SMS messages so the solution should be able to expand to work with that as well if possible.

This blog post presents a very effective (though somewhat 'hacky') solution to your problem that will work in a shared hosting environment. This is what Jeff used in StackOverflow to assign badges to users (I don't know if SO is still using it though).
For the code to actually send the email, you should look around the Internet since there are endless code examples on how to do that. One possible answer could be:
public void SendEmail()
{
MailMessage loMsg = new MailMessage();
loMsg.From = new MailAddress("from#domain.com");
loMsg.To.Add(new MailAddress("to#domain.com"));
loMsg.Subject = "Subject";
loMsg.Body = "Email Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("username", "password")
};
smtp.Send(loMsg);
}
Take a look and see if it helps

#chobo2, you can use quartz.net to check for due tasks (say, every one minute) and take some action (like sending an email) when a task has a due date less than the current date and the task was not notified.
So you will have to have a due date property in your task and a bit indicating if it has been notified.
Every minute, you run code that looks for tasks with due date less than or equal than the current date and for each of them you send a notification email. Then you mark the task as notified.
Regards.

Related

Amazon WorkMail timeout .net core SmtpClient

I am trying to send a standard email message through AWS WorkMail using the SmtpClient in .NET Core. The configuration is very standard according to Amazon Documentation:
https://docs.amazonaws.cn/en_us/general/latest/gr/workmail.html
"Smtp": {
"MailServer": "smtp.mail.eu-west-1.awsapps.com",
"MailPort": "465",
"SenderName": "Us us us",
"FromAddress": "email#domain.com",
"Username": "email#domain.com",
"Password": "Password1$",
"EnableSsl": "true"
},
The settings are injected and the SmtpClient gets properly instantiated and the sending of the email is just:
var mail = new MailMessage
{
From = new MailAddress(_smtpSettings.FromAddress, _smtpSettings.SenderName),
SubjectEncoding = Encoding.UTF8,
BodyEncoding = Encoding.UTF8,
IsBodyHtml = true,
Body = message,
Subject = subject,
Priority = MailPriority.High
};
mail.To.Add(new MailAddress(sendToEmail));
_smtpClient.Send(mail);
Sadly, the sending always fails with a gateway timeout. We tried switching for 587 for STARTTLS and providing the Username without the domain (#). The server sending has proper SSL certificate installed and the mailserver is exactly the one we have our smtp on. Although this would not be a relevant solution, I also increased the timeout to 10 seconds (even though this would not be a solution).
Any idea what could be wrong? I am tearing my hair already. Seems to be an issue with AWS WorkMail.
Just adding to the existing comment. SMTPClient is actually obsolete/deprecated but is not being marked as so. See comments here (https://github.com/dotnet/dotnet-api-docs/issues/2986#issuecomment-430805681)
Essentially it boils down to SmtpClient hasn't been updated in years and is missing many features. The .NET Core team wanted to mark it as obsolete, but some developers have existing projects with "Warnings as Errors" turned on. It would instantly make any project that is using SmtpClient with Warnings as Errors turned on suddenly stop building. So... It's kinda deprecated, but not being marked so in some official docs.
MailKit is actually being pushed by Microsoft full stop for people to use when it comes to Email. Much to some developers chargrin who don't want to use a third party library for such a "simple" and common feature. Just in my personal experience, I think Mailkit is great and super easy to use. A quick guide to getting up and running is here : https://dotnetcoretutorials.com/2017/11/02/using-mailkit-send-receive-email-asp-net-core/
So the problem is that the default .NET Core SmtpClient does not support StartSSL (implicit SSL) which is the only accepted option by WorkMail. You see, WorkMail allows only connections that start from SSL and the SmtpClient first starts from unencrypted and then switches over to encrypted if it cannot connect.
If you are trying this, you will not get this to work using standard SmtpClient and as usually the case with Microsoft, they don't recognize it as an issue. You can either try tunneling or better just use one of the available libraries. Sadly, most of them are paid, there is AIM but it doesn't work with .NET Core and I didn't want to spend time porting not my own library to .NET Standard so I ended up using Mailkit.
There are some issues with the library though, first, before sending you have to call Connect which takes host and port as parameter, that means you cannot just inject premade smtpclient as singleton and have to instantiate it within the place of usage. That's dope and no interface also makes it unmockable which might blow your integration tests. Moreover, you have to do an ugly line before calling send like so:
emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
But at least it works.

Trying to send smtp email in C#. No errors but does not get received. PHP works

I have a server that runs IIS and PHP. I have numerous webpages that send emails, some to me, some to the users. This all works great. I am on a Comcast Business Class account which means I can use smtp.comcast.net as my SMTP server, use port 25, and not use any sort of authentication which is great. And it works just fine.
Now fast forward to today. I am writing some custom C# code to monitor a folder structure and basically email me the new file if it matches certain parameters. In my C# code, I try to use the same settings, but it doesn't work. The SmtpClient.Send() function does not throw an Exception and my code completes the routine as if everything is happy and working. But then I wait and wait and wait, and I never receive the email.
SmtpClient smtp = new SmtpClient("smtp.comcast.net");
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
smtp.Timeout = 2500;
smtp.Send(mail);
onStatusUpdate("Successfully sent email to " + mail.To + (mail.CC.Count > 0 ? " and CC'd " + mail.CC.ToString() : ""));
The "mail" object is of type MailMessage and is setup with the To, From, subject, body, and CC. Also has HTML and Plain Text alternate views.
I guess the easiest question, is if there is a trick to sending email the oldschool port-25 way in C# that doesn't exist in PHP?
And the only reason I mention PHP is because I know my firewall isn't blocking port 25, I know my ISP has it open, I know I have the right server, I know it should work.
I don't know if this affects their Business Class accounts, but Comcast just recently (<2 months ago) closed off port 25 for all their email accounts. try using port 587
http://customer.comcast.com/help-and-support/internet/email-client-programs-with-xfinity-email/
The first place to look is in the mail server logs of the outgoing mail server that you are using to send this message. These should tell you whether or not the mail server is even receiving the message from your C# program for queuing, and if so - what's happening when it attempts to deliver the message to the remote MTA.
Well about 5 hours after I started testing, I got all my test emails at once including the embedded HTML and everything else. So it is working just as it should. I guess since the signature was slightly different they block them until they are deemed non-spam. It also appears that now when I send an email it goes through instantly.
So Comcast has some sort of time delay filter apparently for anybody else in the future with this problem.
I ended up adding mails to the (to list),
and put sending in foreach for every mail I send mail separately,
and it worked!

Sending email through MS Outlook and disabling the warning

I have a C# program that I will be running on a daily basis (through Windows Scheduler). The program is to send a daily report to my team.
I have written the following to send the email and it works. the only problem is that Outlook shows a message box " A program is trying to send an e-mail message on your behalf. if this is unexpected...... " . there are three buttons "allow" "deny" "help" and it seems like my program is halted at that point and until i click the allow or deny button , the program doesn't send the email.
I know that the i can change the options by going into tools -> trust center -> programmatic access, but i would really like to not use that because this program would be eventually running from another machine where the user may or may not access to change the setting in trust center.
Is there a way to disable this warning programatically? ..or is there another way to send the email without having this warning popup
here is the code used to send the email..and it works fine..
Application olook = new Application();
NameSpace ns = olook.GetNamespace("MAPI");
ns.Logon(null, null, true, true);
_MailItem msg = (_MailItem)olook.CreateItem(OlItemType.olMailItem);
msg.To = "xxx#xxx.com";
msg.Subject = "test";
msg.HTMLBody = strHTML;
msg.Send();
ns.Logoff();
there are several ways to do that
you could disable the popup like #DJ KRAZE described
or you could send a message via smtp, if thats possible in your environment
see this: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
then you could use the "redemption library" i've used it and there will be no messages, because redemption suppresses them (or works around them) but the library is used via com, thats not that comfortable..
although you have to pay for that:
http://www.dimastr.com/redemption/home.htm
the thir alternative is using the managed Exchange Web Services
http://www.microsoft.com/download/en/details.aspx?id=13480
this is pretty straight forward and fun to use. you can get that via NuGet as well. :)
EDIT:
i forgot to mention, that Exchange Web Services are only available on Exchange 2007 SP1 or higher.
and this is what it looks like to send a message (after connect to the server)
EmailMessage message = new EmailMessage(service);
message.Subject = "Hello from the EWS Managed API";
message.Body = "Now that's easy!";
message.ToRecipients.Add("someone#fabrikam.com");
message.Save();
look here for an introduction: http://msdn.microsoft.com/en-us/library/dd637749(v=exchg.80).aspx
One of the easiest solutions is to use Exchange's SMTP server. Here's an example from MSDN.
string to = "jane#contoso.com";
string from = "ben#contoso.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = #"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.UseDefaultCredentials = true;
client.Send(message);
Of course, you'll have to check with your Exchange administrator to make sure that SMTP is enabled.

What could cause a message sent from Gmail SMTP using C# not to arrive - No exception is thrown

I am trying to send mail through my Google Apps email account. This is setup on my own domain and is all working fine through the web interface and through outlook.
However, i'm trying to send an email from a webpage using C#, I get no exceptions and everything appears to go smoothly, but the emails never seem to arrive:
MailMessage msg = new MailMessage("no-reply#xxxx.co.uk", "dan#xxxx.co.uk");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("no-reply#xxxx.co.uk", "xxxxxxxxx");
smtp.EnableSsl = true;
msg.IsBodyHtml = true;
msg.Body = body;
smtp.Send(msg);
The body is a string which i've generated earlier in the code. As far as i'm aware, if there was a problem with actually connecting to the SMTP server at Gmail then I would get an exception thrown.
Any ideas what could cause this not to work? Only thing different I can see compared to other peoples examples is that I have set body as HTML and i'm sending from a Google Apps account rather than an #gmail.com account.
The google apps itself is all configured fully and working, i've set this up numerous times so I know it's not likely to be a problem there.
I also tried sending on port 25, as that's what you use when configuring Outlook to send from a Gmail account.
Same result for both, no exception thrown, but email never arrives. Both emails the sender and receiver are on my domain using Google Apps for Gmail.
EDIT:
Also I should mention that I have signed in using both accounts, and both have IMAP and POP enabled in their settings etc.
New findings
This is a strange one
If I send mail manually from:
no-reply#mydomain.com to dan#mydomain.com - Then it works
But if I send it through code this way...it doesn't work...
If I send the following through code it does work:
no-reply#mydomain.com to me#gmail.com or me#ntlworld.com - This works through code!
I would then be led to think that this means a problem with dan#mydomain.com receiving messages...But it receives any messages sent manually from any google, hotmail, or ntlworld email address i've tried.
So either Google Apps accounts can't receive email sent through code (unlikely) or something else is at play here
The server may silently discard your message if there is a problem with it.
- Despite any spec saying otherwise.
The message might be lost in the ether despite being "delivered".
The SMTP server may be applying severe filtering and might additionally require that your sender and destination email addresses match up 'correctly'
I suggest trying with a different SMTP host just to check. :)
When you setup Google Apps, even if the MX records are all working correctly for sending mail manually to/from other accounts, there can still be problems.
The answer is to wait 12-24 hours after setting up your MX Records, even if everything else is working fine.
If you receive no exception, then to me this is the only answer at the moment.
All is working correctly now. Even though all MX records were correct on my DNS and email appeared to be working, there were still changes going on in the background

Using Webbrowser control with Web proxy servers dynamically with C#

I've been following this site with a lot of admiration especially on how questions are professionally answered so I decided to be fully involved.
Please, I need urgent help on a project that I have been working on for a long time but it's almost stalled now just because of a critical issue.
An aspect of the program automates email sending to clients using the free email server systems. Due to the high frequency of email sending, I observed that the email server we're sending to drops larger parts of the emails sent out and literally blocks delivery of major emails to the recipients.
I have tried to reduce the rate of sending email out but to no avail. My fear now is my IP address might have been blocked or may be blocked soon if this continue. The program is not spamming but have to be developed in order to contact a large database of recipients at a goal within short time - like about 1000 or more recipients.
I am using Webbrowser control in C# to automate the process of logging in to the mail server and sending the email out.
Now, what I want is a sample code to use publicly available web proxy servers for each email sent out such that the source IP address appears dynamic and different to the target email server each time a message is sent out to it.
I mean, I want to dynamically get and use free public proxy servers with the Webbrowser control to send out the emails. In this way I believe the email servers would not be able to reject the emails base on the IP address source. I want to know how to dynamically get literally one web proxy server for each email sent, if possible each time.
This project is very critical and this feature is a determinant. I have googled endlessly without any straight forward solution to this issue. I would, therefore, appreciate any useful help, sample codes or resources that could help me to solve this nagging problem once and for all.
Thank you!
Your problem is "free email server systems": they consider you a spammer, and the idea you suggest (spoofing IPs) will, if detected, ruin your reputation.
If you explain what you are trying to accomplish, perhaps someone here can offer a better design.
Are you trying to give people with free email accounts (like Hotmail) bulk-emailing capabilities?
First of all (if I understood your answer right), you don't have to use WebBrowser control - you can use specified .NET solutions that allows you to efficiently sending mails:
MailMessage msg = new MailMessage("from", "to", "subject", "body text");
SmtpClient client = new SmtpClient("smtp server");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("user", "password");
client.UseDefaultCredentials = false;
client.Credentials = cred;
Client.Send(msg);
Unfortunately, if you want to send e-mails to many recipients and you want to be sure, that these messages reach the recipients - you have to do it using your own e-mail server or do it by purchase the service on a paid e-mail servers - then they will not treat you as a spammer.
But if you anyway want to send e-mails by rotation proxy servers or similar sollution - you can define your proxy:
SmtpClient client = new SmtpClient("my.proxy_server.com", 8080);
First you have to collect any list of available proxy servers which allows you to do it in reasonable time (servers switching can significantly increase total process time because conection time can be different for each proxy server)
Proxy servers list ordered by access time:
http://www.publicproxyservers.com/proxy/list_avr_time1.html

Categories

Resources