邮件是否通过Java邮件api成功发送 [英] mail is sent successfully or not by java mail api

查看:443
本文介绍了邮件是否通过Java邮件api成功发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面编写了一个通过java mail api发送邮件的程序,该程序现在可以发送邮件.我的查询是为了处理特殊情况,还可以说,如果未发送邮件,那么我必须提供一些东西,如果邮件已经发送给我,必须在cas中做其他事情,现在请告知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

emailSession.setDebug(true);

请告知java邮件api发送的返回参数是哪个,我们可以通过该参数检查邮件是否成功发送

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 Mail API简单程序

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

推荐答案

我认为您正在寻找以下JavaMail FAQ条目:

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?

这篇关于邮件是否通过Java邮件api成功发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆