Hi all,
I am trying to send and email to through my company mail server. Following is my code
package com.tbss;
import javax.mail.*; import javax.mail.internet.*;
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*;
class tester {
public String toMD5(String string){
byte[] defaultBytes = string.getBytes(); try{ MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(defaultBytes); byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i=0;i<messageDigest.length;i++) { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } String foo = messageDigest.toString(); System.out.println("string "+string+" md5 version is "+hexString.toString()); string=hexString+""; }catch(NoSuchAlgorithmException nsae){ nsae.printStackTrace(); } return string; }
public static void main(String args[]) { tester t=new tester(); String userName=t.toMD5("user"); String password=t.toMD5("password"); System.out.println("user name: "+userName); System.out.println("password: "+password); Properties props = new Properties(); props.put("mail.smtp.host" , "host"); props.put("mail.stmp.user" , userName);
//To use TLS // props.put("mail.smtp.auth", "true"); //props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.password", password); //To use SSL props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.startssl.enable", "true"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "25"); SmtpAuthenticator authenticator=new SmtpAuthenticator(); System.out.println("username: "+authenticator.getPasswordAuthentication().getUserName()); Session session = Session.getInstance( props , authenticator); session.setDebug(true); String to = "[email protected]"; String from = "[email protected]"; String subject = "Testing..."; Message msg = new MimeMessage(session); try { msg.setFrom(new InternetAddress(from)); msg.setRecipient(Message.RecipientType.TO , new InternetAddress(to)); msg.setSubject(subject); msg.setText("Working fine..!"); Transport transport = session.getTransport("smtp"); transport.connect( "host" , 25 , userName , password ); transport.send(msg); System.out.println("fine!!"); } catch(Exception exc) { System.out.println(exc); }
} static class SmtpAuthenticator extends Authenticator { public SmtpAuthenticator() {
super(); } @Override public PasswordAuthentication getPasswordAuthentication() { tester t=new tester(); String username = t.toMD5("user"); String password = t.toMD5("password"); if ((username != null) && (username.length() > 0) && (password != null) && (password.length () > 0)) { return new PasswordAuthentication(username, password); } return null; } }
}
When i run this program i am getting following exception:
DEBUG: setDebug: JavaMail version 1.4.4 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "mail.tata-bss.com", port 25, isSSL false 220 IHGWTMEX07.SerWizSol.com Microsoft ESMTP MAIL Service ready at Wed, 21 Dec 2011 14:26:07 +0530 DEBUG SMTP: connected to host "host", port: 25
EHLO A5173423E.SerWizSol.com 250-IHGWTMEX07.SerWizSol.com Hello [10.64.170.164] 250-SIZE 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-X-ANONYMOUSTLS 250-AUTH NTLM 250-X-EXPS GSSAPI NTLM 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250-XEXCH50 250 XRDST DEBUG SMTP: Found extension "SIZE", arg "" DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "DSN", arg "" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg "" DEBUG SMTP: Found extension "AUTH", arg "NTLM" DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "BINARYMIME", arg "" DEBUG SMTP: Found extension "CHUNKING", arg "" DEBUG SMTP: Found extension "XEXCH50", arg "" DEBUG SMTP: Found extension "XRDST", arg "" DEBUG SMTP: Attempt to authenticate DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: mechanism LOGIN not supported by server DEBUG SMTP: mechanism PLAIN not supported by server DEBUG SMTP: mechanism DIGEST-MD5 not supported by server DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "mail.tata-bss.com", port 25, isSSL false 220 IHGWTMEX07.SerWizSol.com Microsoft ESMTP MAIL Service ready at Wed, 21 Dec 2011 14:26:08 +0530 DEBUG SMTP: connected to host "mail.tata-bss.com", port: 25
EHLO A5173423E.SerWizSol.com 250-IHGWTMEX07.SerWizSol.com Hello [10.64.170.164] 250-SIZE 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-X-ANONYMOUSTLS 250-AUTH NTLM 250-X-EXPS GSSAPI NTLM 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250-XEXCH50 250 XRDST DEBUG SMTP: Found extension "SIZE", arg "" DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "DSN", arg "" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg "" DEBUG SMTP: Found extension "AUTH", arg "NTLM" DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "BINARYMIME", arg "" DEBUG SMTP: Found extension "CHUNKING", arg "" DEBUG SMTP: Found extension "XEXCH50", arg "" DEBUG SMTP: Found extension "XRDST", arg "" DEBUG SMTP: Attempt to authenticate DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: mechanism LOGIN not supported by server DEBUG SMTP: mechanism PLAIN not supported by server DEBUG SMTP: mechanism DIGEST-MD5 not supported by server javax.mail.AuthenticationFailedException: 250-IHGWTMEX07.SerWizSol.com Hello [10.64.170.164] 250-SIZE 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-X-ANONYMOUSTLS 250-AUTH NTLM 250-X-EXPS GSSAPI NTLM 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250-XEXCH50 250 XRDST
Please help me in resolving this problem.
Thanks, Suresh
Please visit the following link: