3

I'm using this code to send e-mails to mail boxes.

private String sendFromGMail(String from, String pass, String[] to, String subject, String body)
    {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        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");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try
        {
            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++)
            {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for (int i = 0; i < toAddress.length; i++)
            {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae)
        {
            ae.printStackTrace();
        }
        catch (MessagingException me)
        {
            me.printStackTrace();
        }

     return "message is send";
    }

I'm interested how I can inset some check is this e-mail successfully send. For example is there any way to check is there any Exception? If there no Exception to return "Mail is send".

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • You want just return status string from this method? – cybersoft Jun 08 '15 at 18:05
  • possible duplicate of [In a finally block, can I tell if an exception has been thrown](http://stackoverflow.com/questions/10736238/in-a-finally-block-can-i-tell-if-an-exception-has-been-thrown) –  Jun 08 '15 at 18:12

4 Answers4

3

Look here: Java Mail API. Basically you can do 2 things:

  1. Implement and Register a TransportListener
  2. Catch these exceptions:

SendFailedException - if the send failed because of invalid addresses. MessagingException - if the connection is dead or not in the connected state

For example:

TransportListener listener = new MyTransportAdapter();
transport.addTransportListener(listener);

where:

class MyTransportAdapter extends TransportAdapter {
//Implement only methods of interest from TransportAdapter API
}
ACV
  • 9,964
  • 5
  • 76
  • 81
  • Looks like a very good solution. Can you show me some code example please? – Peter Penzov Jun 08 '15 at 18:09
  • You just need to add another class. Sorry I cannot do the work for you. For this purposes there are freelancing sites. Good luck. – ACV Jun 08 '15 at 18:15
  • If you send an email to someguy@somecompany.com, Gmail will check if somecompany.com does exists, but it will not know if someguy is there (only the server of somecompany.com know that). You won't get any exception and your message won't be delivered. Look here : http://www.oracle.com/technetwork/java/javamail/faq/index.html#badaddr – Nyamiou The Galeanthrope Jun 08 '15 at 18:16
1

You can add these returns, for example:

...
catch (AddressException ae)
{
    ae.printStackTrace();
    return "Mail is not sent for some reason 1";
}
catch (MessagingException me)
{
    me.printStackTrace();
    return "Mail is not sent for some reason 2";
}
cybersoft
  • 1,453
  • 13
  • 31
1

Just add a boolean and a finally bloc, like this :

try{
   .... 
   boolean isExceptionThrown = false ;
}
catch (AddressException ae){
   ae.printStackTrace();
   isExceptionThrown = true ;
}
catch (MessagingException me)
{
   me.printStackTrace();
   isExceptionThrown = true ;
}
finally{
   if (isExceptionThrown == false) return "mail OK sent" ;
}
0

If you don't have an exception, it only means that the SMTP server have accepted to deliver your message and will try to deliver it. But the adress may not even exist and the only way to know that is to wait and see if you get a "Delivery Status Notifications" telling you that, but some server will not even send you those.

So basically, if you need a reliable way to send a message, don't use emails.