Questions with C# Visual Studios Email notification

TheHome

Member
Joined
Nov 11, 2013
Messages
248
Reaction score
0
Hi all, I have some questions that i am unsure of. Currently I am working on a project, that i need to send email notifications to a user email via Visual Studio 2012. So, I have this web storage called Xively, whereby I want to set my criteria on when the email will be sent. Is that possible?

My codes are as follows;

private void getDatastreamLatestTimestamp(string requestURL)
{
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time");
DateTime dt = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);
string jsonString = HttpRequest.getRequest(requestURL, null);

if (jsonString != null)
{
Datastream ds = JsonConvert.DeserializeObject<Datastream>(jsonString);
DateTime dsTimestamp = TimeZoneInfo.ConvertTime(Convert.ToDateTime(ds.at), timeZoneInfo);

// Compare current datetime with Xively retrieved datetime,
// send out email if difference is more than 2 hours
if (dt.Subtract(dsTimestamp).Hours >= 2)
{
sendEmail();
}
}
}

// Method for sending out email via Gmail service
private void sendEmail()
{
Debug.Print("Sending out email...");
var fromAddress = new MailAddress("12345@gmail.com", "Motion Sensor Web Monitoring");
var toAddress = new MailAddress("121212@gmail.com", "Whom It May Concern");
const string fromPassword = "administrator";
const string subject = "Unusual Motion Timing";
const string body = "Hi, there has been unusual motion timing detected, kindly take note.";

var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
 

javaongsan

Junior Member
Joined
Feb 14, 2008
Messages
27
Reaction score
0
code is correct. The only thing you need to take note is the trigger, if your host allows schedule jobs, or are you manually triggering
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top