How to send mail using JAVA Mail.

8 views
Skip to first unread message

Sandeep Sontakke

unread,
Jan 21, 2010, 1:18:56 PM1/21/10
to javatec...@googlegroups.com
Hi,

I have created on feed back from to send a mail .
I used by gmail id to recieve a mail .
but it wont work bcause in both the field of gmail i.e. From and To I m geting my email id.
here is the code that I have used.


public void postMail() throws MessagingException
  {
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "imap.gmail.com");
     props.put("mail.smtp.starttls.enable","true");
     props.put("mail.smtp.auth", "true");
  
    
     //props.put("mail.smtp.port", "993");
     

    Authenticator auth = new SMTPAuthenticator();
    Session session = Session.getDefaultInstance(props, auth);

    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
   

    InternetAddress[] addressTo = new InternetAddress[2];
    /*for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }*/
    addressTo[0] = new InternetAddress("sandeep....@gmail.com");
    addressTo[1] = new InternetAddress(email);
    msg.setRecipients(Message.RecipientType.TO, addressTo);
   
    message = "Hello.......";

    // Setting the Subject and Content Type
    msg.setSubject("Thanks For feed Back");
    msg.setContent("Test Mail ", "text/plain");
    msg.setFrom(new InternetAddress(email));
    Transport.send(msg);
    System.out.println(addressTo[0]+"Mail Sent successfully...11");
   
 
 }
And one Inner Class
private class SMTPAuthenticator extends javax.mail.Authenticator
{

    public PasswordAuthentication getPasswordAuthentication()
    {
        String username = "sandeep....@gmail.com";
        String password = "XXXXXX";
        return new PasswordAuthentication(username, password);
    }
}

}

Thanks & Regards
Sandeep Sontakke.








'

On Thu, Jan 21, 2010 at 3:35 PM, Kiran Gawde <gawde...@gmail.com> wrote:
Hi Sreehari,

     Test Driven Developement is a defineltly a good way for
executing projects.
But i think if you have a reasonable good team of dedicated testing
team then the writing the test cases and the development goes on
parallel. Brainstorming sessions do happen initially with the entire
team before the test cases are written or the development team starts
writing the Program specs so that there is a clear understanding of
the requirement.
The Test cases are reviewed so that they are meeting the requirements.
    When developemnt team gives the release the test cases are
executed.

So writing test cases and then starting the developemnt is not a
feasible task.

Regards,
Kiran




On Jan 3, 7:12 am, javatechnicals <javatec...@googlegroups.com>
wrote:
> What is Test Driven Development?
>  This is question asked by one of my friend. Here i am writing my
> answer for that. Test Driven Development means writing Test Cases
> first and then developing code. This is best way to achieve a test
> suit. Its time consuming process, but in my open source project and my
> personal recomendation is to follow Test Driven Development. There are
> lots of reason i have found for that from the opensource project i
> have written and i am writing now.
>
> i). Any time you can change the internal structure of the project
> without changing the functionality. Your test cases  makes sure that
> your chages is right or wrong.
>
> ii). Writing test cases will makes you to break down the code
> according to functionality. and ensures that that part is tested
> succesfully
>
> iii). If a new person, who knows the project partially can add/change
> the code[no need to worry about wether its effect to other part of
> project]
>
> iv). Doing TDD we can tell exact persentage of project status.
>
> Some disadvantages are:
>
> i). its boring task, not a creative work
>
> ii) time consuming while following a faster schedule.
>
> iii). Developer must have a good understanding on the automated
> testing API[JUnit or TestNG,..]
>
> This all are my understandings that i got from books my experiance.
> please reply your comments on this

Rejin Chandran

unread,
Jan 21, 2010, 10:48:14 PM1/21/10
to javatec...@googlegroups.com
<sandeep>
but it wont work bcause in both the field of gmail i.e. From and To I m geting my email id.
here is the code that I have used</sandeep>
 
<Rejin>Is it working for diff id, suppose the sending mail id is urs and the recving is someone else. </Rejin>

 

Rejin Chandran

unread,
Jan 22, 2010, 12:34:12 AM1/22/10
to javatec...@googlegroups.com
I think it hangs some where while sending. So set a time out value as i shown below
 props.put("mail.smtp.connectiontimeout",timeOutVal);
Then you will get exact error.
 
Thanks and Regards
Rejin Chandran R

 

Effective Java

unread,
Jan 23, 2010, 2:14:35 AM1/23/10
to javatechnicals
Thank you so much I was also looking out for this code.
Hari

On Jan 22, 10:34 am, Rejin Chandran <reji...@gmail.com> wrote:
> I think it hangs some where while sending. So set a time out value as i
> shown below
>  props.put("mail.smtp.connectiontimeout",timeOutVal);
> Then you will get exact error.
>
> Thanks and Regards
> Rejin Chandran R
>
> On 1/22/10, Rejin Chandran <reji...@gmail.com> wrote:
>
>
>
> > <sandeep>
> > but it wont work bcause in both the field of gmail i.e. From and To I m
> > geting my email id.
> > here is the code that I have used</sandeep>
>
> > <Rejin>Is it working for diff id, suppose the sending mail id is urs and
> > the recving is someone else. </Rejin>
>
> >>     addressTo[0] = new InternetAddress("sandeep.sonta...@gmail.com");

> >>     addressTo[1] = new InternetAddress(email);
> >>     msg.setRecipients(Message.RecipientType.TO<http://message.recipienttype.to/>,

> >> addressTo);
>
> >>     message = "Hello.......";
>
> >>     // Setting the Subject and Content Type
> >>     msg.setSubject("Thanks For feed Back");
> >>     msg.setContent("Test Mail ", "text/plain");
> >>     msg.setFrom(new InternetAddress(email));
> >>     Transport.send(msg);
> >>     System.out.println(addressTo[0]+"Mail Sent successfully...11");
>
> >>  }
> >> And one Inner Class
> >> private class SMTPAuthenticator extends javax.mail.Authenticator
> >> {
>
> >>     public PasswordAuthentication getPasswordAuthentication()
> >>     {
> >>         String username = "sandeep.sonta...@gmail.com";

> >>         String password = "XXXXXX";
> >>         return new PasswordAuthentication(username, password);
> >>     }
> >> }
>
> >> }
>
> >> Thanks & Regards
> >> Sandeep Sontakke.
>
> >> '
>

Srinivas V

unread,
Apr 12, 2010, 4:16:08 PM4/12/10
to javatec...@googlegroups.com
One can configure Gmail in outlook as well as Thunderbird.
The advantage is you Request for Receipt which usually gmail/Yahoo does not provide.
Further backup of mail you have on your machine.
 
Regards,
Srinivas V
Reply all
Reply to author
Forward
0 new messages