简体   繁体   English

邮件是否通过Java Mail API成功发送

[英]mail is sent successfully or not by java mail api

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 我在下面编写了一个通过java mail api发送邮件的程序,该程序现在发送邮件。我的查询是为了处理特殊情况,还可以说如果未发送邮件,那么我必须要处理一些事情,如果要发送邮件,我必须做该cas中的其他内容,现在请告知java mail api在发送邮件购买时是否向我们证明了任何参数,我们可以检查邮件是否已成功发送,因为我已在程序中启用了调试功能

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 请告知java邮件api发送的返回参数是哪个,通过它我们可以检查邮件是否成功发送

below is my simple program of java mail api 下面是我的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();
        }
    }
}

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. 您可以通过比较邮件的内容来检查messag对象是否已发送电子邮件。

I hope it will help you. 希望对您有帮助。

I'm not sure this is what you need. 我不确定这是您所需要的。 But you can check the return code of SMTP server like this: 但是您可以像这样检查SMTP服务器的返回码:

// 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM