【问题标题】:Transport.send() takes 15 minutes to completeTransport.send() 需要 15 分钟才能完成
【发布时间】:2015-10-08 09:28:36
【问题描述】:

嗯,我知道这个问题已经在这里早些时候被问过。我已经看到了提供的所有解决方案和输入。但我无法解决这个问题。 之前相同的代码运行良好,现在每个发送调用大约需要 15 -20 分钟才能真正执行。

我认为我传递了正确的论点。谁能告诉我为什么要花这么多时间?

这是日志文件:

C:\Users\Digital Jalebi - uno\Desktop\naval.txt
Fetched from filenavaljosh@gmail.com;nn.jpg
Email:navaljosh@gmail.comimage:C:\Users\Digital Jalebi - uno\Desktop\naval\nn.jpg
Filling details
send to mail auth file
navaljosh@gmail.com
lenth receipient2
Filling final msg
1
2
3
4
< here it waits for 15 minutes - on transport.send() and then gets executed>
5
From: Navaljoshi <navaljosh@gmail.com>
Reply-to: Navaljoshi <navaljosh@gmail.com>
To: jabongnbajam@gmail.com
Subject: Fwd: EXTRA 32% Off on 2Lac+ Styles
Sent: Thu Oct 08 11:18:14 IST 2015
javax.mail.internet.MimeMultipart@72e1bc00
message forwarded ....
Sucessfully Sent mail to All Users
Fetched from filenull
sleeping1
woke1
Fetched from filenull
sleeping1

代码:

    public class SendMailUsingAuthentication {

        private String HOST_NAME = "gmail-smtp.l.google.com";
        String messageBody;

        @SuppressWarnings("restriction")
        public void postMail(String recipients[], String subject, String message,
                String from, String emailPassword, String files) throws MessagingException {
            boolean debug = false;
            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
     try{
            //Set the host smtp address
            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", HOST_NAME);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");


            Authenticator authenticator = new SMTPAuthenticator(from,emailPassword);
            Session session = Session.getDefaultInstance(props, authenticator);

            session.setDebug(debug);

            // create a message
            Message msg = new MimeMessage(session);

            // set the from and to address
            InternetAddress addressFrom = new InternetAddress(from);
            msg.setFrom(addressFrom);
            System.out.println(addressFrom);

            InternetAddress[] addressTo = new InternetAddress[recipients.length];
            System.out.println("lenth receipient"+recipients.length);
            for (int i = 0; i < recipients.length; i++) {
                addressTo[i] = new InternetAddress(recipients[i],false);
            }

            System.out.println("Filling final msg");
            msg.setRecipients(Message.RecipientType.TO, addressTo);

            // Setting the Subject and Content Type
            msg.setSubject(subject);
           // msg.setContent(message, "text/html");

            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(message);
            System.out.println("1");

            Multipart multipart = new MimeMultipart();

            //add the message body to the mime message
            multipart.addBodyPart(messageBodyPart);
            System.out.println("2");

            // add any file attachments to the message
            addAtachments(files, multipart);
            System.out.println("3");
            //Put all message parts in the message
            msg.setContent(multipart);
            System.out.println("4");
            Transport.send(msg);
     }
     catch(MessagingException e)
     {
         e.printStackTrace();
         System.out.println("in catch mesg exp");
     }
            System.out.println("5");
            ForwardMail frw = new ForwardMail();
            frw.emaiTo = recipients[0];
            ForwardMail.sent();
            System.out.println("Sucessfully Sent mail to All Users");
        }

        protected void addAtachments(String attachments, Multipart multipart)
                throws MessagingException, AddressException {
           // for (int i = 0; i <= attachments.length - 1; i++) {
                String filename = attachments;
                MimeBodyPart attachmentBodyPart = new MimeBodyPart();
                //use a JAF FileDataSource as it does MIME type detection
                DataSource source = new FileDataSource(filename);
                attachmentBodyPart.setDataHandler(new DataHandler(source));
                attachmentBodyPart.setFileName(filename);
                //add the attachment
                multipart.addBodyPart(attachmentBodyPart);
          //  }
        }

【问题讨论】:

  • '进入无限循环' != '实际执行大约需要 15-20 分钟'。是哪个?
  • @Asmi 为什么您的编辑删除了日志?请勿破坏此处的帖子。
  • 它进入一个循环,实际上在 - 分钟内完成。
  • 不要将猜测作为事实发布。你不知道它在内部做什么。如果它完成,它不能处于无限循环中。这肯定很明显吗?最重要的是,它需要很长时间才能完成。
  • 嗯,这就是问题所在,为什么要花这么多时间,一开始就没有??

标签: java android sockets jakarta-mail


【解决方案1】:

您可以通过修复所有这些common mistakes 来清理您的程序,然后关注这个example for using Gmail

如果这不能解决您的问题(我认为不会),请打开JavaMail Session debugging;通过观察调试输出暂停的位置,您可能能够确定它在减速时正在做什么。问题很可能是名称服务问题(查找 Gmail 的主机名),或者是防火墙或防病毒程序干扰了您的连接尝试。

【讨论】:

    【解决方案2】:

    您正在设置 mail.smtp.host 两次。

    props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.host", HOST_NAME);

    仅使用一台主机进行测试并查看。

    您也可以添加一个属性来查看邮件服务器连接是否有问题。

    properties.put("mail.smtp.connectiontimeout", "15000");

    P

    【讨论】:

    • 没有成功,尝试了你的两个建议。最初它工作得很好..如果我等待 15 分钟,它仍然会。
    • mail.smtp.connectiontimeout 属性值以毫秒为单位,因此为 15 秒。你是什​​么意思最初它工作得很好。您使用的是哪个 javamail jar 文件。
    猜你喜欢
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 2013-05-08
    相关资源
    最近更新 更多