Send email

747 views
Skip to first unread message

Thibaud GODARD

unread,
Aug 4, 2011, 9:04:42 AM8/4/11
to play-framework
Hi,

I'm trying to set up an app which users can sends emails to webmaster
("contact us").
I want to use my Google account to send the emails through Google SMTP-
server. But I can't make it work.

This is my mail configuration:
# Mail configuration
# ~~~~~
# Default is to use a mock Mailer
#mail.smtp=mock
mail.smtp=smtp.gmail.com
mail.smtp.user=tee...@gmail.com
mail.smtp.pass=********
mail.smtp.channel=ssl
mail.smtp.port=587

My mail fonction:

public static void sendMail(String adress, String message){
SimpleEmail email = new SimpleEmail();

email.setFrom(adress);
email.addTo("teest...@gmail.com");
email.setSubject("Demande d'inscription partenaire");
email.setMsg(message);
Mail.send(email);

System.out.println("Email envoye!");
//validInscription();
}

Hope someone can help me :p

Ahmed Mahmoud

unread,
Aug 4, 2011, 10:06:17 AM8/4/11
to play-fr...@googlegroups.com
I donnu but this may be the problems =D

mail.smtp.host=smtp.gmail.com


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--

Ahmed Mahmoud Mohammed Abd El-wahab 
el shaheer b Ahmed Fat7y.

Vijay Kiran

unread,
Aug 4, 2011, 9:36:17 AM8/4/11
to play-framework
Are you getting any exceptions ? Can you post the log ?

On Aug 4, 3:04 pm, Thibaud GODARD <thibaud.god...@ysance.com> wrote:
> Hi,
>
> I'm trying to set up an app which users can sends emails to webmaster
> ("contact us").
> I want to use my Google account to send the emails through Google SMTP-
> server. But I can't make it work.
>
> This is my mail configuration:
> # Mail configuration
> # ~~~~~
> # Default is to use a mock Mailer
> #mail.smtp=mock
> mail.smtp=smtp.gmail.com
> mail.smtp.user=tee...@gmail.com
> mail.smtp.pass=********
> mail.smtp.channel=ssl
> mail.smtp.port=587
>
> My mail fonction:
>
> public static void sendMail(String adress, String message){
>                 SimpleEmail email = new SimpleEmail();
>
>                 email.setFrom(adress);
>                 email.addTo("teestysa...@gmail.com");

Thibaud GODARD

unread,
Aug 4, 2011, 11:33:03 AM8/4/11
to play-framework
Yes I have exceptions:

ERROR ~

@67affd32k
The email has not been sent

Mail error
A mail error occured : Error while sending email

play.exceptions.MailException: Error while sending email
at play.libs.Mail$2.call(Mail.java:183)
at play.libs.Mail$2.call(Mail.java:175)
at java.util.concurrent.FutureTask
$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor
$Worker.runTask(ThreadPoolExec
utor.java:886)
at java.util.concurrent.ThreadPoolExecutor
$Worker.run(ThreadPoolExecutor
.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.apache.commons.mail.EmailException: Sending the email
to the foll
owing server failed : localhost:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:
1242)
at org.apache.commons.mail.Email.send(Email.java:1267)
at play.libs.Mail$2.call(Mail.java:180)
... 6 more
Caused by: javax.mail.MessagingException: Could not connect to SMTP
host: localh
ost, port: 465;
nested exception is:
java.net.ConnectException: Connection refused: connect
at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:52
5)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:
1232)
... 8 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:
351)
at
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at
com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java
:559)
at
com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketI
mpl.java:141)
at
com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at
com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:201)
at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 15 more

Thibaud GODARD

unread,
Aug 4, 2011, 11:34:59 AM8/4/11
to play-framework
I tried : mail.smtp*.host*=smtp.gmail.com but that don't work :/

Ahmed Mahmoud

unread,
Aug 4, 2011, 11:37:19 AM8/4/11
to play-fr...@googlegroups.com
Caused by: javax.mail.MessagingException: Could not connect to SMTP
host: localh
ost, port: 465;

I don't kno why does it connect to the localhost =( although that line "mail.smtp.host=smtp.gmail.com" tells it the host explicitly :(

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Thibaud GODARD

unread,
Aug 4, 2011, 11:46:02 AM8/4/11
to play-framework
Yes, I don't anderstand...

On Aug 4, 5:37 pm, Ahmed Mahmoud <thediamondknig...@gmail.com> wrote:
> *Caused by: javax.mail.MessagingException: Could not connect to SMTP
> host: localh**ost, port: 465;*
> *
> *
> I don't kno why does it connect to the localhost =( although that line "*
> mail.smtp.host=smtp.gmail.com" *tells it the host explicitly :(

Rakesh Waghela

unread,
Aug 4, 2011, 12:03:46 PM8/4/11
to play-fr...@googlegroups.com
 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 = {"t...@gmail.com"}; // 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();

Rakesh Waghela

unread,
Aug 4, 2011, 12:06:23 PM8/4/11
to play-fr...@googlegroups.com
For all advanced usage.This library is best !

Thibaud GODARD

unread,
Aug 5, 2011, 3:18:49 AM8/5/11
to play-framework
Thank you but I wanted to try the mail module from play!
Reply all
Reply to author
Forward
0 new messages