Mono, C# Sending mail through gmail

Hello I’m back at blog posting after some busy time!

I know this example is nothing to do with mono but since I use mono these days this example also develop this in SharpDevelop+Mono+gtk#, My aim is simple just send a mail through my gmail account…

Let’s code than:

using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Text;
using Gtk;
using GtkSharp;
using GLib;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace KentSoft
{
class  printTest : Window
{
public  printTest()
: base("Kent_Calisma")
{

try{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("yourmailadress@gmail.com");
mail.To.Add("destinationmailadress@gmail.com");
mail.Subject = "TEST";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("gmailusername without @gmail.com", "gmailpassword");
SmtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
SmtpServer.Send(mail);
}
catch(Exception e){
Console.WriteLine("Ouch!"+e.ToString());
}
}
public static void Main()
{
Application.Init();
new printTest();
Application.Run();
}
}
}

Output:

mailoutputThats all folks for sending mail from C# using gmail!
Become a Friend of GNOME
kick it on DotNetKicks.com

About these ads