Send E-Mail from your .NET application using your GMail Account [转]

  

Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report this article.

Introduction

Most independent developers like to include a msg sending feature in their application. Well atleast I tried to.. Of course it is not a problem for a user with a smtp server access or a registered domain with an smtp server access. But unfortunately I had none of them. I use msn, gmail and yahoo for my mailing needs. Unfortunately yahoo does not offer smtp or pop access for their general/basic/free users. And msn it is not that reliable coz I was expecting a lot of mails to be sent and also to recive and as far as I know they also donot give you pop/smtp access!.

Typical Example you will find in internet

So I really Hunted for a code segment that help me use my free gmail account to send E-MAIL from my .NET account. http://www.systemwebmail.com is a complete referance library and FAQ's regarding to the System.Web.Mail namespase. And my code is basically I joined different pieces together. And Came up with succes. Normally you will find thousands of sites offering the code below which has a very little use. Like

 MailMessage mail = new MailMessage(); mail.To = "xxxx@mailserver.com"; mail.Bcc = "yyyy@mailserver.com; user3@mailserver.net"; mail.From = "your email or name"; mail.Subject = "your subject"; mail.Body = "your subject"; SmtpMail.SmtpServer = "10.4.1.2"; //your real server goes here or //you can add the address like "smtp.mailserver.com" SmtpMail.Send(mail);

 

Well most smtp server require authentication and sometimes SSL connection so what then? The code above is USELESS. So what are the things you need to add? There you go.

How to use

Copy the above methods and paste in visual studio IDE or any text editor you use. Add a referance to System.Web or System.Web.Mail dll change your account info like your mail address, password, etc and Enjoy

Explanation to the code

So what is happening on the example? See
step 1: Create a mail msg object and asign the basic parts like from, To, cc, bcc subject, body.
step 2: Add some config settings in fields collection of the mailMessage object. Type of authentication we use basic authentication and your user name, password, smtp servers port number that you will be using. for example:
gmail username require full gmail address as username and you can use /connect to any of the port.
step 3: This is the coolest and most important part that I had a lot of time to spend to discover.

mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); //Cool. Ha? Donot Change! This part is a must if you want to access GMAIL's SMTP server

This line says/declares that your smtp server requires SSL connection which is very much true/necessary for accessing to gmail's SMTP server.
step 4: assign the smtp server name in System.Web.Mail.SmtpMail class's Smtp server object like:

SmtpMail.SmtpServer = "smtp.gmail.com";

you can also set the priority of your mail by

mailMsg.Priority = MailPriority.High;// MailPriority is an enum. It has High,Low, Normal option

step 5: call the send method of System.Web.Mail.SmtpMail. e.g.

System.Web.Mail.SmtpMail.Send(myMailMsg);

 

.NET2.0 Version

Some Changes are required for .NET v2.0 as .NET2.0 Mail is much organised and stable and so many extrafeatures. So In the Update I am including .NET2.0 version of my code.

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Mail;using System.Net.Mime; //Mime is Not necerrary if you dont change the msgview and //if you dont add custom/extra headers using System.Threading;namespace SendMailUsingGmail{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }                static bool mailSent = false;        public void SendMail()        {            //Builed The MSG            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();            msg.To.Add("reciver@gmail.com");            msg.To.Add("another.reciver@yahoo.com");            msg.From = new MailAddress("dummy@gmail.com", "One Ghost",System.Text.Encoding.UTF8);            msg.Subject = "Test mail using .net2.0";           msg.SubjectEncoding = System.Text.Encoding.UTF8;            msg.Body = "This is my msg Body";            msg.BodyEncoding = System.Text.Encoding.UTF8;            msg.IsBodyHtml = false;            msg.Priority = MailPriority.High;                                   //Add the Creddentials            SmtpClient client = new SmtpClient();            client.Credentials = new System.Net.NetworkCredential("dummy@gmail.com", "SecretPass");            client.Port = 587;//or use 587                        client.Host = "smtp.gmail.com";            client.EnableSsl = true;            client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);            object userState=msg;            try            {                //you can also call client.Send(msg)                client.SendAsync(msg, userState);                            }            catch (System.Net.Mail.SmtpException ex)            {                MessageBox.Show(ex.Message, "Send Mail Error");            }        }        void client_SendCompleted(object sender, AsyncCompletedEventArgs e)        {            MailMessage mail = (MailMessage)e.UserState;            string subject = mail.Subject;            if (e.Cancelled)            {                string cancelled = string.Format("[{0}] Send canceled.", subject);                MessageBox.Show(cancelled);                            }            if (e.Error != null)            {                string error = String.Format("[{0}] {1}", subject, e.Error.ToString());                MessageBox.Show(error);                            }            else            {                MessageBox.Show("Message sent.");            }            mailSent = true;        }        private void button1_Click(object sender, EventArgs e)        {            this.SendMail();        }    }}

Limitation of this code

This code may/will not work with .NET framework v1.0. Because the MailMessage object of .NETv1.0 does not have the MailMessage.Fields Collection. This was added in .NETv1.1. So it works fine with .NETv1.1 and v2.0.

conclusion

The send method does not return anything. But you can add some mechanism(like counter) to keep track of send message or even keep the return type of your method as a mailmessage type so that u can save to database for security reasons.

The reason I could not provide a complete send and also recive example because I am very busy with my Thesis project. But I also wanted to publish the code sample to code project so anyone may find it helpfull. I will publish a complete article about sending and reciving mail using gmail account from your gmail account

Future upgrade Plan

I am planning to upload another complete article for both sending and reciveing E-Mail's of gmail account from a single application with complete source code. I need some feedback about which to choose ASP.NET code or windows programming sample or both? and I am gonna add some config file to dynamically change Mail settings like sender mail a/c, SMTP server name, default port number and priority etc. send feedback moshiur.m@gmail.com

//basic authenticationmailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//sender email addressmailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myemail@gmail.com");//plaintext password. //you can encrypt your password and keep in a config file when you parse just decrypt itmailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");// port number - smtp.gmail.com use port 465 or 587. use anyone of them
you can even make some grouping so different groups use different port to share the load on servermailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");   

//This is fully working tested code. Copy and paste the code//Edit some parts(UserName, password) and ENJOY!public string sendMail (string from, string to, string cc, string bcc, string subject, string body) {    // Mail initialization    MailMessage mailMsg = new MailMessage();    mailMsg.From = from;    mailMsg.To = to;    mailMsg.Cc = cc;    mailMsg.Bcc = bcc;    mailMsg.Subject = subject;    mailMsg.BodyFormat = MailFormat.Text;    mailMsg.Body = body;    mailMsg.Priority = MailPriority.High;    // Smtp configuration    SmtpMail.SmtpServer = "smtp.gmail.com";    // - smtp.gmail.com use smtp authentication    mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");    mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myemail@gmail.com");    mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");    // - smtp.gmail.com use port 465 or 587    mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");    // - smtp.gmail.com use STARTTLS (some call this SSL)    mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");    // try to send Mail    try     {        SmtpMail.Send(mailMsg);        return "";    }    catch (Exception ex)     {        return ex.Message;    }}

 

posted @ 2007-01-09 23:08  Kevin Lin  阅读(685)  评论(0编辑  收藏  举报