junaidp
junaidp

Reputation: 11201

How to send email in java GWT

I am using this code in my java GWT application

        public String greetServer(String input) throws Exception {
    try{
    Properties props = new Properties();

     props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.smtp.port", "25");
    props.setProperty("mail.host", "smtp.random.com");
    props.setProperty("mail.user", "[email protected]");
    props.setProperty("mail.password", "000000000");

    Session mailSession = Session.getDefaultInstance(props, null);
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject("hello");
    message.setContent("helloo sss", "text/plain");
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));

    transport.connect();
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();
    } catch(NoSuchProviderException e){
        throw new Exception(e);
      }

    return input;

}

Error: javax.mail.MessagingException: Could not connect to SMTP host: smtp.random.com, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect

if i use

            props.setProperty("mail.host", "smtp.live.com");
            and use my hotmail account , it gives this error 

           javax.mail.MessagingException: can't determine local email address

Any idea what could be the solution

thanks

Upvotes: 0

Views: 1245

Answers (4)

Steven Jay Cohen
Steven Jay Cohen

Reputation: 73

I just used Simple Java Mail in a GWT project. You might want to try it. It's very simple to configure.

There are lots of example there, including one of sending using gmail's SMTP server using for example TLS:

Email email = new Email.Builder()
    .from("Michel Baker", "[email protected]")
    .to("mom", "[email protected]")
    .to("dad", "[email protected]")
    .subject("My Bakery is finally open!")
    .text("Mom, Dad. We did the opening ceremony of our bakery!!!")
    .build();

new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email);

If you have two-factor login turned on, you need to generate an application specific password from your Google account.

Upvotes: 2

NickJ
NickJ

Reputation: 9559

Here's some Gmail settings which work for me:

//these are fed into the Properties object below:
mail.smtp.starttls.enable = true
mail.transport.protocol   = smtp
mail.smtp.auth            = true

and some Java:

Properties properties = ...

javax.mail.Session session = javax.mail.Session.getInstance(properties, null);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", username, password);

Upvotes: 0

Limeoats
Limeoats

Reputation: 426

Error: javax.mail.MessagingException: Could not connect 
to SMTP host: smtp.random.com port: 25; 
nested exception is: java.net.ConnectException: Connection refused: connect

This error means that your SMTP server you provided is invalid. The code you have is correct, but smtp.random.com must not be a valid SMTP server.

I suggest you look into using Google's SMTP server that is free provided you use a valid gmail account.

Refer to this page for more information on using Gmail's STMP server: http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm

Upvotes: 0

Related Questions