Java Transport.send()需要15分钟才能完成
我知道早些时候有人问过这个问题。我看到了提供的所有解决方案和投入。但我无法解决这个问题。 早些时候,同样的代码工作正常,现在每个发送调用实际执行大约需要15-20分钟 我认为我的论点是正确的。有人能告诉我为什么要花这么多时间吗 以下是日志文件:Java Transport.send()需要15分钟才能完成,java,android,sockets,jakarta-mail,Java,Android,Sockets,Jakarta Mail,我知道早些时候有人问过这个问题。我看到了提供的所有解决方案和投入。但我无法解决这个问题。 早些时候,同样的代码工作正常,现在每个发送调用实际执行大约需要15-20分钟 我认为我的论点是正确的。有人能告诉我为什么要花这么多时间吗 以下是日志文件: C:\Users\Digital Jalebi - uno\Desktop\naval.txt Fetched from filenavaljosh@gmail.com;nn.jpg Email:navaljosh@gmail.comimage:C:\U
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
C:\Users\Digital Jalebi-uno\Desktop\naval.txt
从filenavaljosh@gmail.com;nn.jpg
电邮:navaljosh@gmail.comimage:C:\Users\Digital Jalebi-uno\Desktop\naval\nn.jpg
填充细节
发送到邮件验证文件
navaljosh@gmail.com
第二次会议
填充最终味精
1.
2.
3.
4.
<在此等待15分钟-在transport.send()上,然后执行>
5.
发件人:Navaljoshi
答复:纳瓦尔乔希
致:jabongnbajam@gmail.com
主题:Fwd:2Lac+款式额外32%折扣
发送时间:2015年10月8日星期四11:18:14
javax.mail.internet。MimeMultipart@72e1bc00
消息已转发。。。。
已成功向所有用户发送邮件
从filenull获取
睡觉1
woke1
从filenull获取
睡觉1
代码:
public类SendMailUsingAuthentication{
私有字符串HOST_NAME=“gmail smtp.l.google.com”;
字符串消息体;
@禁止警告(“限制”)
public void postMail(字符串收件人[]、字符串主题、字符串消息、,
字符串发件人、字符串emailPassword、字符串文件)引发MessaginException{
布尔调试=假;
java.security.security.addProvider(新的com.sun.net.ssl.internal.ssl.Provider());
试一试{
//设置主机smtp地址
Properties props=新属性();
props.put(“mail.transport.protocol”、“smtp”);
props.put(“mail.smtp.port”,“465”);
props.put(“mail.smtp.starttls.enable”、“true”);
props.put(“mail.smtp.host”,主机名);
props.put(“mail.smtp.auth”,“true”);
put(“mail.smtp.host”、“smtp.gmail.com”);
props.put(“mail.smtp.socketFactory.port”,“465”);
put(“mail.smtp.socketFactory.class”、“javax.net.ssl.SSLSocketFactory”);
Authenticator Authenticator=新的SMTPAuthenticator(发件人,emailPassword);
Session Session=Session.getDefaultInstance(道具、验证器);
setDebug(debug);
//创建消息
Message msg=新的mimessage(会话);
//设置发件人和收件人地址
InternetAddress addressFrom=新的InternetAddress(from);
msg.setFrom(addressFrom);
System.out.println(addressFrom);
InternetAddress[]addressTo=新的InternetAddress[recipients.length];
System.out.println(“长度接收者”+接收者.length);
for(int i=0;i //对于(int i=0;i您将设置mail.smtp.host两次
put(“mail.smtp.host”、“smtp.gmail.com”);
props.put(“mail.smtp.host”,主机名)
仅使用一台主机进行测试,请参阅
此外,还可以添加属性以查看邮件服务器连接是否存在问题
properties.put(“mail.smtp.connectiontimeout”、“15000”)
p您可以通过修复所有这些问题,然后执行以下操作来清理程序
如果这不能解决您的问题(我认为不会),请打开;通过观察调试输出暂停的位置,您可能能够确定当速度减慢时它在做什么。最有可能的问题是名称服务问题(查找Gmail的主机名),或防火墙或反病毒程序介入您的连接尝试。“进入无限循环”!=“实际执行大约需要15-20分钟”。是哪一个?@Asmi为什么您的编辑会删除日志?不要破坏此处的帖子。它进入循环,实际上在-分钟内完成。不要将猜测作为事实发布。您不知道它在内部做什么。如果它完成了,它不可能处于无限循环中。这当然是显而易见的?突出的是它需要很长时间才能完成。这就是问题,为什么它需要那么多时间,最初它没有成功?没有成功,尝试了你的两个建议。最初它工作得很好。但如果我等15分钟,它会的。mail.smtp.connectiontimeout属性值是以毫秒为单位的,所以它是15秒。最初它工作正常是什么意思?您使用的是哪个javamail jar文件。
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);
// }
}