Solved

Send email through gmail account

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

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now