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:





Alessandro 1:57 pm on August 30, 2010 Permalink |
Hello
tank you very much for the code.
Is the only that work very fine with gmail smtp client….
thanks
huseyincakir 7:10 pm on August 31, 2010 Permalink |
have a look: http://csharpmail.codeplex.com/
Chaagalar 1:59 pm on January 29, 2011 Permalink |
Hi
I got and error (The remote certificate is invalid according to the validation procedure) when I didin’t use “ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };” this line fixes my problem.
Thank you Huseyin.
mEERAN 8:22 am on October 31, 2012 Permalink |
Thanks