JavaMail application hanged with no error throwed at Transport.send,even though I set the timeout property
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
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="sender";
/**
* @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);
}
MailData mailData = new MailData();
mailData.setSubject("altireport mail configuration");
mailData.setContent("mail server has been configured successfully.");
mailData.setRecipients(new String[]{"nwan@altigen.com.cn"});
final Session session = Session.getInstance(props, authenticator);
final MimeMessage msg = new MimeMessage(session);
InternetAddress from = new InternetAddress(sender);
msg.setFrom(from);
// msg.setSender(from);
final InternetAddress[] addressTo = new InternetAddress[mailData.getRecipients().length];
for (int i = 0; i < mailData.getRecipients().length; i++) {
addressTo[i] = new InternetAddress(mailData.getRecipients());
}
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());
// msg.saveChanges();
for(int i=0;i<10;i++){
new Thread(new Runnable(){
public void run() {
try {
System.out.println("send...");
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;
}
}
I have tried use session to get a SMTPTransport instance to use sendMessage ,but still have the same problem.No exception ,No error.
This problem doesn't appear always. It appears sometimes.
I hope get help for someone who has the solution for this problem.
Thanks in advanced.