Biswajit das
Biswajit das

Reputation: 51

How to check if the mail is not sent using java code

I am using the following code to send mail

public class Mail
{
    public static void main(String[] args)
    {
                String[] to={"[email protected]"};
                String[] cc={"[email protected]"};
                String subject = "Subject";
                String body = "This is Body....!!";

        Mail.sendMail("[email protected]","Password","smtp.gmail.com","465","true",
       "true",true,"javax.net.ssl.SSLSocketFactory","false",to,cc,
       subject,body);             
    }



  public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String[] to,String[] cc,String subject,String text)
{
            Properties props = new Properties();
            //Properties props=System.getProperties();
    props.put("mail.smtp.user", userName);
    props.put("mail.smtp.host", host);
            if(!"".equals(port))
    props.put("mail.smtp.port", port);
            if(!"".equals(starttls))
    props.put("mail.smtp.starttls.enable",starttls);
    props.put("mail.smtp.auth", auth);
            if(debug){
            props.put("mail.smtp.debug", "true");
            }else{
            props.put("mail.smtp.debug", "false");         
            }
            if(!"".equals(port))
    props.put("mail.smtp.socketFactory.port", port);
            if(!"".equals(socketFactoryClass))
    props.put("mail.smtp.socketFactory.class",socketFactoryClass);
            if(!"".equals(fallback))
    props.put("mail.smtp.socketFactory.fallback", fallback);

    try
    {
                    Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);
        MimeMessage msg = new MimeMessage(session);
        msg.setText(text);
        msg.setSubject(subject);
        msg.setFrom(new InternetAddress("[email protected]"));
                    for(int i=0;i<to.length;i++){
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
                    }
                    for(int i=0;i<cc.length;i++){
        msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
                    }
        msg.saveChanges();
                    Transport transport = session.getTransport("smtp");
                    transport.connect(host, userName, passWord);
                    transport.sendMessage(msg, msg.getAllRecipients());
                    transport.close();
                    return true;
    }
    catch (Exception mex)
    {
        mex.printStackTrace();
                    return false;
    }
    }

The code is working fine and the message is been sent. But I want to set a flag (like flag=false) if the messsage has not been sent (like when I try to send to [email protected] since its not valid mail Id). Would really appreciate if someone could help me on this.

Thanks in advance

Upvotes: 0

Views: 2591

Answers (2)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41220

You should pass Authenticator object while creating the Session.

Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

authenticator - Authenticator object used to call back to the application when a user name and password is needed.

Please find working example with explanation - Link

Upvotes: -1

Navin
Navin

Reputation: 4107

You will probably have to put an actual email in msg.setFrom() and check its inbox for a Non delivery report.

Most servers will try to send your email several times before giving up so you can't figure out if the email is valid instantly.

Upvotes: 2

Related Questions