mail is sent successfully or not by java mail api

不羁岁月 提交于 2020-01-06 17:09:07

问题


I have written below a program to send an mail through java mail api which send the mail now my query is to handle the exceptional scenarios also lets say if mail is not sent then i have to da something and if mail is send i have to do some other thing in that cas , now please advise does java mail api proved us any parameter while sending the mail buy which we can check that mail is been sent successfully or not as i have enabled the debugging in my program

emailSession.setDebug(true);

please advise which is the parameter in return sent by java mail api by which we can check mail is sent successfully or not

below is my simple program of java mail api

public class EmailTest {

    public static void main(String[] args) {

        String mailSmtpHost = "cakelycakes.com";

        String mailTo = "bigcakes@cakelycakes.com";
        String mailCc = "littlecakes@cakelycakes.com";
        String mailFrom = "me@here.there.everywhere";
        String mailSubject = "Email from Java";
        String mailText = "This is an email from Java";

        sendEmail(mailTo, mailCc, mailFrom, mailSubject, mailText, mailSmtpHost);
    }

    public static void sendEmail(String to, String cc, String from, String subject, String text, String smtpHost) {
        try {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", smtpHost);
            Session emailSession = Session.getDefaultInstance(properties);

            Message emailMessage = new MimeMessage(emailSession);
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            emailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
            emailMessage.setFrom(new InternetAddress(from));
            emailMessage.setSubject(subject);
            emailMessage.setText(text);

            emailSession.setDebug(true);

            Transport.send(emailMessage);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

回答1:


I think you're looking for these JavaMail FAQ entries:

  • If I send a message to a bad address, why don't I get a SendFailedException or TransportEvent indicating that the address is bad?
  • When a message can't be delivered, a failure message is returned. How can I detect these "bounced" messages?



回答2:


well there is a way to get all the email sent.

for ex-

      //create the POP3 store object and connect with the pop server
  Store store = emailSession.getStore("pop3s");

  store.connect(host, user, password);

  //create the folder object and open it
  Folder emailFolder = store.getFolder("INBOX");
  emailFolder.open(Folder.READ_ONLY);

  // retrieve the messages from the folder in an array and print it
  Message[] messages = emailFolder.getMessages();
  System.out.println("messages.length---" + messages.length);

  for (int i = 0, n = messages.length; i < n; i++) {
     Message message = messages[i];
     System.out.println("---------------------------------");
     System.out.println("Email Number " + (i + 1));
     System.out.println("Subject: " + message.getSubject());
     System.out.println("From: " + message.getFrom()[0]);
     System.out.println("Text: " + message.getContent().toString());

  }

  //close the store and folder objects
  emailFolder.close(false);
  store.close();

  } catch (NoSuchProviderException e) {
     e.printStackTrace();
  } catch (MessagingException e) {
     e.printStackTrace();
  } catch (Exception e) {
     e.printStackTrace();
  }

}

you can check the messag object whether the email has sent or not by comparing its contents.

I hope it will help you.




回答3:


I'm not sure this is what you need. But you can check the return code of SMTP server like this:

// get your configuration (host, port, user, pwd)
...
// initialize your message
...

SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect(host, port, user, pwd);
transport.sendMessage(message, message.getAllRecipients());
// you can get SMTP return code here
int code = transport.getLastReturnCode();


来源:https://stackoverflow.com/questions/30632960/mail-is-sent-successfully-or-not-by-java-mail-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!