【发布时间】: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