I wanted to write my own automated behaviour after I receive new email and for that I needed to read emails that I get.
I have used OpenPop Nuget package.
Here is my package.config file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenPop.NET" version="2.0.6.1116" targetFramework="net45" />
</packages>
Now after installing the package I have needed to add implementation that I took from
https://github.com/foens/hpop/blob/master/OpenPopExamples/Examples.cs to read emails.
Here is my code with configuration.
// The client disconnects from the server when being disposed
using (Pop3Client client = new Pop3Client())
{
// Connect to the server
client.Connect("pop.gmail.com", 995, true);
// Authenticate ourselves towards the server
client.Authenticate("email@gmail.com", "password");
// Get the number of messages in the inbox
int messageCount = client.GetMessageCount();
// Most servers give the latest message the highest number
for (int i = messageCount; i > 0; i--)
{
var msg = client.GetMessage(i);
Console.WriteLine(msg.Headers.Subject));
}
}
Now the issue is that email does not allow me to read the emails.
And the reading fill fail with exception.
If you drill into the exception you will be able to find following link
https://www.google.com/settings/security/lesssecureapps
This will give you configuration option to configure this
Now when I run the code I get my emails through.
Showing posts with label Email. Show all posts
Showing posts with label Email. Show all posts
Sunday, 26 July 2015
Friday, 2 November 2012
MVC and Sending email
I have often needed email class that works with my website.
Here I am adding small memory nudge :)
How do you setup your webconfig?
how does website use the SmtpClient ?
Here is your configuration of web.config:
Here I am adding small memory nudge :)
How do you setup your webconfig?
how does website use the SmtpClient ?
Here is your configuration of web.config:
<system.net> <mailSettings> <smtp deliveryMethod="Network" from="name@domain.com"> <network host="smtp.mail.com" userName="name@domain.com" password="blog.dotnetclr.com" port="25"/> </smtp> </mailSettings> </system.net>
Note: this tag is straight in configuration tag of your web.config
And here is the sample C# code that will use the above configuration settings
public void SendBy(string to, string subject, string body) { var mailMessage = new System.Net.Mail.MailMessage(); mailMessage.To.Add(to); mailMessage.Subject = subject; mailMessage.Body = body; var smtpClient = new SmtpClient(); smtpClient.EnableSsl = true; smtpClient.Send(mailMessage); }
Subscribe to:
Posts (Atom)