views:

25

answers:

1

I am writing some application in which i need to send mails. My application is in JAVA so probably i will be using javamail. But i dont know how to use it and how to configure mail server from my local pc so that i can test if mails can be sent.Please suggest.

+2  A: 

you can also use GMAIL smtp to send mail.
Here is a sample snippet

String host = "smtp.gmail.com";
    String from = "username";
    String pass = "password";
    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true"); // added this line
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    String[] to = {"[email protected]"}; // added this line

    Session session = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));

    InternetAddress[] toAddress = new InternetAddress[to.length];

    // To get the array of addresses
    for( int i=0; i < to.length; i++ ) { // changed from a while loop
        toAddress[i] = new InternetAddress(to[i]);
    }
    System.out.println(Message.RecipientType.TO);

    for( int i=0; i < toAddress.length; i++) { // changed from a while loop
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
    }
    message.setSubject("sending in a group");
    message.setText("Welcome to JavaMail");
    Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
org.life.java
any dependencies on google. Can i send mail to any email address.
sushil bharwani
@sushil bharwani, yes you can send it to any valid mail address, google's server will get use. that s the only dependency
org.life.java
can use i use google smtp server for production level code. Like i use it for sending mails by 20,000 employees. Sorry if i sound stupid.
sushil bharwani
@sushil bharwani , If you planning to send mail to client or user it won't look good if you will use gmail :) , you can have gmail for enterprise also it will give you @yourdomainname.com ,at last it is your choice
org.life.java
I didnt followed "It wont look good" lets say i am developing application for my company. The mails are send to companies clients and employees. What are the reasons i should not use GMAIL SMTP.
sushil bharwani
@sushil bharwani , if you are normal gmail user than you would probably have mail id something like `[email protected]` if you use this mail id your client ,user will get this mail id as from field, and this doesn't look pretty professional :) I hope it is clear now
org.life.java
Yes thanks very much i followed. just to clarify u said the gmail will be able to give me @mydomain as well.
sushil bharwani
@sushil bharwani Yes,You can get more info about that over here http://www.google.com/apps/intl/en/business/gmail.html,
org.life.java
is something free available....
sushil bharwani
@sushil bharwani No idea. probably they provide it free for limited account like in free version you will get around30 , i am not sure
org.life.java
thanks @org.life.java for all your efforts.
sushil bharwani