Solved

Send email through gmail account

Posted on 2012-03-29
9
347 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
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.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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