繁体   English   中英

如何检查邮件是否未使用java代码发送

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

我使用以下代码发送邮件

public class Mail
{
    public static void main(String[] args)
    {
                String[] to={"abcd@xyz.co"};
                String[] cc={"pqrs@xyz.com"};
                String subject = "Subject";
                String body = "This is Body....!!";

        Mail.sendMail("From@gmail.com","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("From@gmail.com"));
                    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;
    }
    }

代码工作正常,消息已发送。 但是我想设置一个标志(如flag = false)如果没有发送消息(就像我尝试发送到abcd@xyz.com,因为它的无效邮件ID)。 如果有人可以帮助我,我真的很感激。

提前致谢

您可能需要在msg.setFrom()放入一个实际的电子邮件,并检查其收件箱中的非传递报告

大多数服务器会在放弃之前尝试多次发送您的电子邮件,因此您无法确定该电子邮件是否立即生效。

您应该在创建Session传递Authenticator对象。

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

authenticator - 用于在需要用户名和密码时回调应用程序的Authenticator对象。

请找到解释的工作示例 - 链接

暂无
暂无

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

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