Google+ All posts by admin

Peter Bromberg's .NET Blog All Things Programming

How to Send Mail With Gmail And C#

3. June 2021 14:27 by admin in

Many developers have difficulty sending mail through Gmail because of security issues. Google provides what is called an "app-specific password" that can be used if you have 2-factor authentication turned on (you should!).

Here is the URL to set this up: https://myaccount.google.com/apppasswords  And here is some sample C# code to use it. You simply replace your regular gmai password with the app-specific password that you set up in your Google settings.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SendMail
{
class Program
{
static string smtpAddress = "smtp.gmail.com";
static int portNumber = 587;
static bool enableSSL = true;

static string emailFromAddress = "youremail@gmail.com"; //Sender Email Address
static string password = "APPSPECIFICPWD"; //APP SPECIFIC PWD Generated at Google Account
static string emailToAddress = "you@gmail.com"; //Receiver Email Address
static string subject = "Hello";
static string body = "Hello, This is Email sending test using gmail.";

static void Main(string[] args)
{
SendEmail();

}

public static void SendEmail()
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFromAddress);
mail.To.Add(emailToAddress);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// mail.Attachments.Add(new Attachment("C:\\Temp\\TestFile.txt"));//--Uncomment this to send any attachment

using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFromAddress, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
}

}
}

Track Yahoo Finance News with Blazor

28. March 2021 16:13 by admin in BLAZOR

RapidAPI publishes a lot of different APIs including many from Yahoo Finance. Limited accounts are usually free.   You can get the full source code for this app at github here:

  Github Repo

 For this i used the stock Blazor Server app template from Visual Studio 2019, and stripped out everything related to the stock "Weather Service" that it creates. Then I added a Finance News model class, and a FinanceNews Service class. Both appear below.

 

 

 

 

How to block tracking cookies in Google Chrome

27. April 2018 15:08 by admin in CHROME, SECURITY

 

Web sites plant cookies for all kinds of reasons, some good, and many not so good. If you visit the New York Times, for example, there are nearly 80 cookies planted, many from services that do nothing but collect your browsing habits and track you all over the web.

1. Click the favicon at the left of the address bar. If it's https, click the green "Secure" icon. You'll see a popup with a list of options. 
2. Near the bottom will be "cookies" Click that and you will see the screen in the photo. From here you can block any cookies you don't like. You're welcome.

NOTE: If you like to stay signed in to your Google account, don't block any Google cookies.

 

Lexical Analysis (Word Count) of Trump's State of the Union Speech

31. January 2018 08:30 by admin in

Each  year I do an analysis of the word count of the current president's State of the Union addresss. The code to do this (once you have downloaded the speech as a text file) has gotten much simpler over the years, especially after the introduction of LINQ.

Here is an example of the key part of the code in C# as an extension method:

public static Dictionary<string, int> GetWordFrequency(this string input){
return input.Split(new char[] { ' ' })
.Where(i => i.Trim() != String.Empty && Regex.IsMatch(i,@"\w"))
.Select(i => Regex.Replace(i,@"[^A-Za-z0-9]+$","").ToLower())
.Where(x => !stopwords.Contains(x))
.GroupBy(w => w)
.OrderByDescending(group => group.Count())
.ToDictionary(group => group.Key, group => group.Count());
}

Here is the sorted list of words with their frequency (Down to a count of 5):

american,29
people,23
americans,20
tonight,20
america,14
congress,13
tax,13
country,13
home,11
am,10
administration,9
america's,9
family,9
world,8
immigration,8
united,7
building,7
safe,7
finally,7
workers,7
nation,7
veterans,7
citizens,7
heroes,6
love,6
strong,6
proud,6
jobs,6
protect,6
communities,6
nuclear,6
isis,6
north,6
passed,5
help,5
police,5
including,5
stands,5
bill,5
reform,5
drugs,5
drug,5
dangerous,5
terrorists,5

 

You can download the complete source code below. The speech.text file is in the /bin/debug folder.

SOTU.zip (215.24 kb)