Sending mail form you Gmail Acount through your Application
Hi
A few days back I wanted to send my mails using the Gmail Account. The problem was that I wanted to automate the system and send mail from my Gmail Account through my Application. For this purpose I wrote a class that would send mail from Gmail Account. Here is the class that I wrote for sending the mail from Gmail.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace SendMailFormGmail
{
    class GmailEmail
    {
        public bool sendMail(string FromEmail,string  FromName,string ToList, string Subject, string Body,string Password)
        {
        MailMessage Msg = new MailMessage();

        ////set message sender
        Msg.From = new MailAddress(FromEmail, FromName);

              ////sets the mail recipients                                 ////person(s) who will receive an email
                ////(in this case 2 person will be emailed)
                Msg.To.Add(new MailAddress(ToList));

         ////set to true if you want to use html in the body
                Msg.IsBodyHtml = true;
                Msg.Subject = Subject;

                 ////body of the email message with html tag
               Msg.Body = Body;

                 ////Allows applications to send e-mail
              //// host server :  smtp.gmail.com
                ////port number : 587
                SmtpClient objMail = new SmtpClient("smtp.gmail.com", 587);

                 ////Some SMTP servers require you to authenticate first
               ////gmail uses SSL     
                ////info object contains the gmail
                ////username, and password
                NetworkCredential info = new NetworkCredential(FromEmail, Password);

                 objMail.DeliveryMethod = SmtpDeliveryMethod.Network;
               objMail.Credentials = info;
                objMail.EnableSsl = true;

            try
            {
                ////final step send email
                objMail.Send(Msg);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

The function sendMail returns a Boolean value on weather the mail was send or not

Hope this helps
Thanks
Vikram



Share this post   Email it

Feedback

Posted on 9/16/2006 11:53:24 PM

In your solution there isn't any information about sending mail exception, only boolean value.
Good idea will be add body email temaplate stroage in file, end only insert into its only some values:

mail.txt:
Hello {0}!

in code replace {0} by user name

Gretings from Poland :)

Posted on 9/17/2006 4:49:31 AM

Hi lowczy
Yes you are right. I also follow a process which is similar to what youy said but a bit different. I will explaing the full stuff in another blog
But to inform you I create the HTML files through editor with some placeholder in It. I replace the placeholder with the data(most of the time from database). This class is supposed to be the base class for sending mail so this replacement is done in the class which used this class.
And about the exception, Yes that is one thing missing. What should be done is add a new property to the clas which decides wheather this class will handle the errors or not (if not then the class should raise any exception that occurs in the function
Thanks
Vikram

Posted on 9/26/2007 12:42:20 PM

In your code -

////Some SMTP servers require you to authenticate first
////gmail uses SSL
////info object contains the gmail
////username, and password
NetworkCredential info = new NetworkCredential(FromEmail, Password);

How do you get Password?

If one is to use this program to send email from different users of application, each with his//her email id//password, what would be the best strategy to access password(s)?

Thanks
Naren

Posted on 9/27/2007 8:53:57 PM

HI Naren,
If some one is to use the above code to send Email then the person needs to have an gmail account. The username and password of the gmail account need to be send to the function to send mail. Gmail account is available free of cost. you can create a new Gmail account and use that to send mail.

Posted on 2/7/2008 4:06:11 AM

Brother this code does not work, its gives message failure sending mail, iam using vs2005

Posted on 2/7/2008 7:13:59 AM

HI Jagjit,

Not sure why it did not work for you. I am using the same to send mail from my site and its working fine. Just check that while running the code, the username and password are correct, You are connected to internet and gmail is not blocked in the network

Posted on 3/16/2008 8:23:48 PM

Vikram, great thanks! This is what I was looking for! You had actually taken good efforts to do that.

CT.

Posted on 5/6/2008 1:57:19 PM

How about VB code for this?

Where do i put this? In the ASP sub folder? web.config?

Posted on 7/20/2008 11:47:34 PM

can i hide from mailid , i mean , i m sending mail using 'smtp.gmail.com' it goes succesfully , but it always goes with gmail loginID ,
////////////////////////////////////////////////////////////////////////////
////info object contains the gmail
////username, and password
NetworkCredential info = new NetworkCredential(FromEmail, Password);
////////////////////////////////////////////////////////////////////
i want to just hide gmail lognID, receiver can understand , it comes from another domain.

Posted on 2/24/2009 1:20:21 AM

Thank You Very Much Vikram. I need this very much. Thank You

Posted on 3/31/2009 2:28:38 AM

hi

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2012 Vikram Lakhotia