Sending mail form you Gmail Acount through your Application
Posted on 8/18/2006 10:09:54 AM
in #ASP.NET 2.X
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;/o:p> using System.Collections.Generic;/o:p> using System.Text;/o:p> using System.Net;/o:p> using System.Net.Mail;/o:p>/span>/p>
namespace SendMailFormGmail/o:p> {/o:p> /span>class GmailEmail/o:p> /span>{/o:p> /span>public bool sendMail(string
FromEmail,string FromName,string ToList, string
Subject, string Body,string Password)/o:p> /span>{/o:p>
/span>MailMessage Msg = new MailMessage();/o:p>/span>/p>
/o:p> /span>////set
message sender/o:p> /span> Msg.From =
new MailAddress(FromEmail, FromName);/o:p>/span>/p>
/o:p> /span>////sets the
mail recipients/o:p> /span> ////person(s)
who will receive an email/o:p> /span>////(in this
case 2 person will be emailed)/o:p>
/span>Msg.To.Add(new MailAddress(ToList));/o:p>/span>/p>
/span>////set to
true if you want to use html in the body/o:p>
/span>Msg.IsBodyHtml = true;/o:p> /span>Msg.Subject
= Subject;/o:p>/span>/p>
/span>////body of
the email message with html tag/o:p> /span>Msg.Body =
Body;/o:p>/span>/p>
/span>////Allows
applications to send e-mail/o:p> /span>//// host
server : /span>smtp.gmail.com/o:p> /span>////port
number : 587/o:p> /span>SmtpClient
objMail = new SmtpClient("smtp.gmail.com", 587);/o:p>/span>/p>
/o:p> /span>////Some SMTP
servers require you to authenticate first/o:p> /span> /span>////gmail uses SSL /span>/o:p> /span>////info
object contains the gmail/o:p> /span>////username,
and password/o:p>
/span>NetworkCredential info = new NetworkCredential(FromEmail, Password);/o:p>/span>/p>
/o:p>
/span>objMail.DeliveryMethod = SmtpDeliveryMethod.Network;/o:p>
/span>objMail.Credentials = info;/o:p>
/span>objMail.EnableSsl = true;/o:p>/span>/p>
/o:p> /span>try/o:p> /span>{/o:p> /span>////final
step send email/o:p>
/span>objMail.Send(Msg);/o:p> /span>return
true;/o:p> /span>}/o:p> /span>catch/o:p> /span>{/o:p> /span>return
false;/o:p> /span>}/o:p> /span>}/o:p> /span>}/o:p> }/span>/p>/span>The function /span>sendMail
/span>returns a Boolean value on weather the mail was send
or not/span> /span>/p>
/p>
Hope this helps Thanks /span>Vikram/p>/span> /span>/o:p>/span>/p>
|
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:
/p>
mail.txt:
Hello {0}!
/p>
in code replace {0} by user name
/p>
Gretings from Poland :)
/p>
|
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
/p>
|
Posted on 9/26/2007 12:42:20 PM
In your code -
/p>
////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);
/p>
How do you get Password?
/p>
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)?
/p>
Thanks
Naren
/p>
|
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.
/p>
|
Posted on 2/7/2008 4:06:11 AM
Brother this code does not work, its gives message failure sending mail, iam using vs2005
/p>
|
Posted on 2/7/2008 7:13:59 AM
HI Jagjit,
/p>
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
/p>
|
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.
/p>
CT.
/p>
|
Posted on 5/6/2008 1:57:19 PM
How about VB code for this?
/p>
Where do i put this? In the ASP sub folder? web.config?
/p>
|
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.
/p>
|
Posted on 2/24/2009 1:20:21 AM
Thank You Very Much Vikram. I need this very much. Thank You
/p>
|
Posted on 3/31/2009 2:28:38 AM
|