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.
views:
25answers:
1
+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
2010-10-27 16:26:32
any dependencies on google. Can i send mail to any email address.
sushil bharwani
2010-10-27 16:30:06
@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
2010-10-27 16:31:26
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
2010-10-27 16:36:17
@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
2010-10-27 16:39:34
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
2010-10-27 16:55:01
@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
2010-10-27 16:58:35
Yes thanks very much i followed. just to clarify u said the gmail will be able to give me @mydomain as well.
sushil bharwani
2010-10-27 17:01:48
@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
2010-10-27 17:08:08
is something free available....
sushil bharwani
2010-10-27 17:25:02
@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
2010-10-28 06:23:30
thanks @org.life.java for all your efforts.
sushil bharwani
2010-10-28 09:36:25