使用JavaMail并发发送邮件时,在Transport.send方法处有时会出现假死状态,程序执行到这里不动了,也不报错,设置的超时参数也没有任何用处,请教各位如何解决这个问题?有时可能不会有,需要多执行几次。使用JavaMail1.4.1和1.4.3都出现这个问题
import java.util.Date;
import java.util.Properties;import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class tt {
static Properties props=null;
static boolean needAuth=true;
static MailAuthenticator authenticator = null;
static String host="host";
static String account="account";
static String password="pwd";
static String sender="from";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
if (props == null) {
props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.timeout ", 1000);
props.put("mail.smtp.connectiontimeout ", 1000);
//props.put("mail.debug", "true");
props.put("mail.smtp.auth", String.valueOf(needAuth));
authenticator = new MailAuthenticator(account, password);
}
System.out.println("get session....");
Session session = Session.getInstance(props, authenticator);
final MimeMessage msg = new MimeMessage(session);
InternetAddress from = new InternetAddress(sender);
msg.setFrom(from);
MailData mailData = new MailData();
mailData.setSubject("altireport mail configuration");
mailData.setContent("mail server has been configured successfully.");
mailData.setRecipients(new String[]{"[email protected]"});
// msg.setSender(from);
InternetAddress[] addressTo = new InternetAddress[mailData.getRecipients().length]; for (int i = 0; i < mailData.getRecipients().length; i++) {
addressTo[i] = new InternetAddress(mailData.getRecipients()[i]);
} msg.addRecipients(Message.RecipientType.TO, addressTo);
//msg.setSubject(mailData.getSubject());
msg.setSubject(MimeUtility.encodeText(mailData.getSubject(), "UTF-8", "B")); MimeBodyPart bodyPart1 = new MimeBodyPart();
bodyPart1.setContent(mailData.getContent(), "text/plain; charset=UTF-8"); MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart1);
msg.setContent(multipart);
msg.setSentDate(new Date());
final Transport ts=session.getTransport(addressTo[0]);
ts.connect();
for(int i=0;i<10;i++){
new Thread(new Runnable(){
public void run() {
System.out.println("send...");
try {
Transport .send(msg);
} catch (Exception e) {
e.printStackTrace(System.out);
}
System.out.println("end!");
}
}).start();
}
}
}class MailData { private String[] recipients = null; private String subject = null; private String content = null; private String attachment = null;
private String attachmentName = null;
/**
* @return the attachment
*/
public String getAttachment() {
return attachment;
} /**
* @param attachment the attachment to set
*/
public void setAttachment(String attachment) {
this.attachment = attachment;
} /**
* @return the content
*/
public String getContent() {
return content;
} /**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
} /**
* @return the recipients
*/
public String[] getRecipients() {
return recipients;
} /**
* @param recipients the recipients to set
*/
public void setRecipients(String[] recipients) {
this.recipients = recipients;
} /**
* @return the subject
*/
public String getSubject() {
return subject;
} /**
* @param subject the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
} /**
* @return the attachmentName
*/
public String getAttachmentName()
{
return attachmentName;
} /**
* @param attachmentName the attachmentName to set
*/
public void setAttachmentName(String attachmentName)
{
this.attachmentName = attachmentName;
}
}
class MailAuthenticator extends Authenticator { private PasswordAuthentication authentication; /**
*
*/
public MailAuthenticator(String account, String password) {
authentication = new PasswordAuthentication(account, password);
} /**
*
*/
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}}
import java.util.Date;
import java.util.Properties;import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class tt {
static Properties props=null;
static boolean needAuth=true;
static MailAuthenticator authenticator = null;
static String host="host";
static String account="account";
static String password="pwd";
static String sender="from";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
if (props == null) {
props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.timeout ", 1000);
props.put("mail.smtp.connectiontimeout ", 1000);
//props.put("mail.debug", "true");
props.put("mail.smtp.auth", String.valueOf(needAuth));
authenticator = new MailAuthenticator(account, password);
}
System.out.println("get session....");
Session session = Session.getInstance(props, authenticator);
final MimeMessage msg = new MimeMessage(session);
InternetAddress from = new InternetAddress(sender);
msg.setFrom(from);
MailData mailData = new MailData();
mailData.setSubject("altireport mail configuration");
mailData.setContent("mail server has been configured successfully.");
mailData.setRecipients(new String[]{"[email protected]"});
// msg.setSender(from);
InternetAddress[] addressTo = new InternetAddress[mailData.getRecipients().length]; for (int i = 0; i < mailData.getRecipients().length; i++) {
addressTo[i] = new InternetAddress(mailData.getRecipients()[i]);
} msg.addRecipients(Message.RecipientType.TO, addressTo);
//msg.setSubject(mailData.getSubject());
msg.setSubject(MimeUtility.encodeText(mailData.getSubject(), "UTF-8", "B")); MimeBodyPart bodyPart1 = new MimeBodyPart();
bodyPart1.setContent(mailData.getContent(), "text/plain; charset=UTF-8"); MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart1);
msg.setContent(multipart);
msg.setSentDate(new Date());
final Transport ts=session.getTransport(addressTo[0]);
ts.connect();
for(int i=0;i<10;i++){
new Thread(new Runnable(){
public void run() {
System.out.println("send...");
try {
Transport .send(msg);
} catch (Exception e) {
e.printStackTrace(System.out);
}
System.out.println("end!");
}
}).start();
}
}
}class MailData { private String[] recipients = null; private String subject = null; private String content = null; private String attachment = null;
private String attachmentName = null;
/**
* @return the attachment
*/
public String getAttachment() {
return attachment;
} /**
* @param attachment the attachment to set
*/
public void setAttachment(String attachment) {
this.attachment = attachment;
} /**
* @return the content
*/
public String getContent() {
return content;
} /**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
} /**
* @return the recipients
*/
public String[] getRecipients() {
return recipients;
} /**
* @param recipients the recipients to set
*/
public void setRecipients(String[] recipients) {
this.recipients = recipients;
} /**
* @return the subject
*/
public String getSubject() {
return subject;
} /**
* @param subject the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
} /**
* @return the attachmentName
*/
public String getAttachmentName()
{
return attachmentName;
} /**
* @param attachmentName the attachmentName to set
*/
public void setAttachmentName(String attachmentName)
{
this.attachmentName = attachmentName;
}
}
class MailAuthenticator extends Authenticator { private PasswordAuthentication authentication; /**
*
*/
public MailAuthenticator(String account, String password) {
authentication = new PasswordAuthentication(account, password);
} /**
*
*/
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货