Solved

Send email through gmail account

Posted on 2012-03-29
9
346 Views
Last Modified: 2012-04-16
Please tell about the workable send email code.
0
Comment
Question by:searchsanjaysharma
9 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37785809
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37785820
use hotmail which is easy to configure with SMTP client and MailMessage.Gmail having more security so its an issue for us.:)


Regards,
nishant
0
 
LVL 4

Expert Comment

by:rar3z
ID: 37785825
Here's a little snippet from one of my code....

Create a class file for simplicity and reuse..

using System;
using System.Web.Mail;

namespace Gmail
{

    public class GmailMessage : System.Web.Mail.MailMessage
    {

        #region CDO Configuration Constants

        private const string SMTP_SERVER = "http://schemas.microsoft.com/cdo/configuration/smtpserver";
        private const string SMTP_SERVER_PORT = "http://schemas.microsoft.com/cdo/configuration/smtpserverport";
        private const string SEND_USING = "http://schemas.microsoft.com/cdo/configuration/sendusing";
        private const string SMTP_USE_SSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl";
        private const string SMTP_AUTHENTICATE = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate";
        private const string SEND_USERNAME = "http://schemas.microsoft.com/cdo/configuration/sendusername";
        private const string SEND_PASSWORD = "http://schemas.microsoft.com/cdo/configuration/sendpassword";

        #endregion

        #region Private Variables

        private static string _gmailServer = "smtp.gmail.com";
        private static long _gmailPort = 465;
        private string _gmailUserName = string.Empty;
        private string _gmailPassword = string.Empty;

        #endregion

        #region Public Members

        /// <summary>
        /// Constructor, creates the GmailMessage object
        /// </summary>
        /// <param name="gmailUserName">The username of the gmail account that the message will be sent through</param>
        /// <param name="gmailPassword">The password of the gmail account that the message will be sent through</param>
        public GmailMessage(string gmailUserName, string gmailPassword)
        {
            this.Fields[SMTP_SERVER] = GmailMessage.GmailServer;
            this.Fields[SMTP_SERVER_PORT] = GmailMessage.GmailServerPort;
            this.Fields[SEND_USING] = 2;
            this.Fields[SMTP_USE_SSL] = true;
            this.Fields[SMTP_AUTHENTICATE] = 1;
            this.Fields[SEND_USERNAME] = gmailUserName;
            this.Fields[SEND_PASSWORD] = gmailPassword;

            _gmailUserName = gmailUserName;
            _gmailPassword = gmailPassword;
        }

        /// <summary>
        /// Sends the message. If no from address is given the message will be from <c>GmailUserName</c>@Gmail.com
        /// </summary>
        public void Send()
        {
            try
            {
                if (this.From == string.Empty)
                {
                    this.From = GmailUserName;
                    if (GmailUserName.IndexOf('@') == -1) this.From += "@Gmail.com";
                }

                System.Web.Mail.SmtpMail.Send(this);
            }
            catch (Exception ex)
            {
                //TODO: Add error handling
                throw ex;
            }
        }

        /// <summary>
        /// The username of the gmail account that the message will be sent through
        /// </summary>
        public string GmailUserName
        {
            get { return _gmailUserName; }
            set { _gmailUserName = value; }
        }

        /// <summary>
        /// The password of the gmail account that the message will be sent through
        /// </summary>
        public string GmailPassword
        {
            get { return _gmailPassword; }
            set { _gmailPassword = value; }
        }

        #endregion

        #region Static Members

        /// <summary>
        /// Send a <c>System.Web.Mail.MailMessage</c> through the specified gmail account
        /// </summary>
        /// <param name="gmailUserName">The username of the gmail account that the message will be sent through</param>
        /// <param name="gmailPassword">The password of the gmail account that the message will be sent through</param>
        /// <param name="message"><c>System.Web.Mail.MailMessage</c> object to send</param>
        public static void SendMailMessageFromGmail(string gmailUserName, string gmailPassword, MailMessage message)
        {
            try
            {
                message.Fields[SMTP_SERVER] = GmailMessage.GmailServer;
                message.Fields[SMTP_SERVER_PORT] = GmailMessage.GmailServerPort;
                message.Fields[SEND_USING] = 2;
                message.Fields[SMTP_USE_SSL] = true;
                message.Fields[SMTP_AUTHENTICATE] = 1;
                message.Fields[SEND_USERNAME] = gmailUserName;
                message.Fields[SEND_PASSWORD] = gmailPassword;

                System.Web.Mail.SmtpMail.Send(message);
            }
            catch (Exception ex)
            {
                //TODO: Add error handling
                throw ex;
            }
        }

        /// <summary>
        /// Sends an email through the specified gmail account
        /// </summary>
        /// <param name="gmailUserName">The username of the gmail account that the message will be sent through</param>
        /// <param name="gmailPassword">The password of the gmail account that the message will be sent through</param>
        /// <param name="toAddress">Recipients email address</param>
        /// <param name="subject">Message subject</param>
        /// <param name="messageBody">Message body</param>
        public static void SendFromGmail(string gmailUserName, string gmailPassword, string toAddress, string subject, string messageBody)
        {
            try
            {
                GmailMessage gMessage = new GmailMessage(gmailUserName, gmailPassword);

                gMessage.To = toAddress;
                gMessage.Subject = subject;
                gMessage.Body = messageBody;
                gMessage.From = gmailUserName;
                if (gmailUserName.IndexOf('@') == -1) gMessage.From += "@Gmail.com";

                System.Web.Mail.SmtpMail.Send(gMessage);
            }
            catch (Exception ex)
            {
                //TODO: Add error handling
                throw ex;
            }
        }

        /// <summary>
        /// The name of the gmail server, the default is "smtp.gmail.com"
        /// </summary>
        public static string GmailServer
        {
            get { return _gmailServer; }
            set { _gmailServer = value; }
        }

        /// <summary>
        /// The port to use when sending the email, the default is 465
        /// </summary>
        public static long GmailServerPort
        {
            get { return _gmailPort; }
            set { _gmailPort = value; }
        }

        #endregion
    }
}

Open in new window


Usage ...

Gmail.SendFromGmail("your_username", "your_password", "To_address", "Subject", "Body");

Open in new window


Or if you already have a MailMessage object you can send that using

Gmail.SendMailMessageFromGmail("your_username", "your_password",  your_MailMessage_object);

Open in new window

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:searchsanjaysharma
ID: 37785987
The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37785995
with gmail or hotmail.??
0
 

Author Comment

by:searchsanjaysharma
ID: 37786033
gmail
0
 

Author Comment

by:searchsanjaysharma
ID: 37786036
using System.Web.Mail;
using System;
public class MailSender
{
    public static bool SendEmail(
        string pGmailEmail,
        string pGmailPassword,
        string pTo,
        string pSubject,
        string pBody,
        System.Web.Mail.MailFormat pFormat,
        string pAttachmentPath)
    {
        try
        {
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");
            //sendusing: cdoSendUsingPort, value 2, for sending the message using
            //the network.

            //smtpauthenticate: Specifies the mechanism used when authenticating
            //to an SMTP
            //service over the network. Possible values are:
            //- cdoAnonymous, value 0. Do not authenticate.
            //- cdoBasic, value 1. Use basic clear-text authentication.
            //When using this option you have to provide the user name and password
            //through the sendusername and sendpassword fields.
            //- cdoNTLM, value 2. The current process security context is used to
            // authenticate with the service.
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
            //Use 0 for anonymous
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                pGmailEmail);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                 pGmailPassword);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 "true");
            myMail.From = pGmailEmail;
            myMail.To = pTo;
            myMail.Subject = pSubject;
            myMail.BodyFormat = pFormat;
            myMail.Body = pBody;
            if (pAttachmentPath.Trim() != "")
            {
                MailAttachment MyAttachment =
                        new MailAttachment(pAttachmentPath);
                myMail.Attachments.Add(MyAttachment);
                myMail.Priority = System.Web.Mail.MailPriority.High;
            }

            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(myMail);
            return true;
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 37796610
http://geekswithblogs.net/TakeNote/archive/2007/07/16/113974.aspx contains code to send email using gmail account.
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37850056
ok
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question