* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws MessagingException, TextParseException, UnsupportedEncodingException {
for(int ai=0;ai<args.length;ai++){
String mailaddr = args[ai];
Session ssn = initMailSession();
MimeMessage mailMessage = new MimeMessage(ssn);
mailMessage.setSubject("Hello MAIL");
mailMessage.setSentDate(new Date());
//Properties props = ssn.getProperties();
//props.put("mail.smtp.from", "<>");
Multipart multipart = new MimeMultipart("related");
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Welcome to JavaMail.");
multipart.addBodyPart(messageBodyPart);
mailMessage.setContent(multipart);
mailMessage.setFrom(new InternetAddress("javayou@gmail.com","Winter Lau"));
String mail_postfix = mailaddr.substring(mailaddr.indexOf('@')+1);
//System.out.println("mail postfix is " + mail_postfix);
Lookup lookup = new Lookup(mail_postfix, Type.MX);
lookup.run();
if (lookup.getResult() != Lookup.SUCCESSFUL){
System.out.println(" " + lookup.getErrorString());
return;
}
Record[] answers = lookup.getAnswers();
for(int i=0;i<answers.length;i++){
Transport transport = null;
//System.out.println("Using " + answers[i].getAdditionalName()+" to send...");
ssn.getProperties().put("mail.smtp.host", answers[i].getAdditionalName().toString());
InternetAddress smtp_host = new InternetAddress(answers[i].getAdditionalName().toString());
try {
transport = ssn.getTransport(smtp_host);
transport.connect();
System.out.println("connect to "+smtp_host+" ok.");
InternetAddress mailToAddress = new InternetAddress(mailaddr);
transport.sendMessage(mailMessage, new InternetAddress[]{mailToAddress});
System.out.println("mail sent to " + mailaddr + " via " + smtp_host);