java mail

hello i m stuck with this code .....runtime exception is giving.....
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.*;
 
class a1{
	public static void main (String[] args) {
   sendJavaMail("smtp.rediffmail.com", "mithunda50@rediffmail.com","mithunda50@rediffmail.com" ,
"mithunda50@rediffmail.com", "mithunda50@rediffmail.com", "mithunda50@rediffmail.com", "mithunda50@rediffmail.com", "text/html");
	}
// This method send a message
     public static boolean sendJavaMail(String host, String from, String to,
String cc, String bcc, String subject, String message, String mimeType)
  {
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
 
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(false);
 
    try
    {
      //Message msg = new MimeMessage(session);
      MimeMessage msg = new MimeMessage(session);
 
      InternetAddress from2 = new InternetAddress(from);
      msg.setFrom(from2);
 
      InternetAddress[] to2 =  InternetAddress.parse(to);
      msg.setRecipients(Message.RecipientType.TO, to2);
 
      if(cc != null)
      {
        if(!(cc.trim().equals("")))
        {
          InternetAddress[] cc2 =  InternetAddress.parse(cc);
          msg.setRecipients(Message.RecipientType.CC, cc2);
        }
      }
 
      if(bcc !=null)
      {
        if(!(bcc.trim().equals("")))
        {
          InternetAddress[] bcc2 =  InternetAddress.parse(bcc);
          msg.setRecipients(Message.RecipientType.BCC, bcc2);
        }
      }
 
      // Create subject
      msg.setSubject(subject);
 
      // Create and fill the first message part
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setContent(message, mimeType);
 
      // Create the Multipart and its parts to it
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(mbp1);
 
      // Add the Multipart to the message
      msg.setContent(mp);
 
      Transport.send(msg);     
               
               return true;
    }
    catch (MessagingException ex)
    {
      ex.printStackTrace();
               
               return false;
    }
  }
	}

Open in new window

mithunda5011Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mithunda5011Author Commented:
and error is....





--------------------Configuration: <Default>--------------------
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
 
    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1294)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:635)
    at javax.mail.Transport.send0(Transport.java:189)
    at javax.mail.Transport.send(Transport.java:118)
    at a1.sendJavaMail(a1.java:65)
    at a1.main(a1.java:9)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
 
    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1192)
    ... 5 more
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
 
    ... 6 more
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first
 
    ... 6 more
 
Process completed.

Open in new window

Ajay-SinghCommented:
rediffmail smtp server requires you to authenticate with the server,
check out this
 
http://www.jguru.com/faq/view.jsp?EID=48895
mithunda5011Author Commented:
sir now i hv modified it like this.......
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.*;
 
class a1{
	public static void main (String[] args) {
   sendJavaMail("smtp.rediffmail.com", "mithunda50@rediffmail.com","mithunda50@rediffmail.com" ,
"mithunda50@rediffmail.com", "mithunda50@rediffmail.com", "mithunda50@rediffmail.com", "mithunda50@rediffmail.com", "text/html");
	}
// This method send a message
     public static boolean sendJavaMail(String host, String from, String to,
String cc, String bcc, String subject, String message, String mimeType)
  {
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(false);
 
    try
    {
      //Message msg = new MimeMessage(session);
      MimeMessage msg = new MimeMessage(session);
 
      InternetAddress from2 = new InternetAddress(from);
      msg.setFrom(from2);
 
      InternetAddress[] to2 =  InternetAddress.parse(to);
      msg.setRecipients(Message.RecipientType.TO, to2);
 
      if(cc != null)
      {
        if(!(cc.trim().equals("")))
        {
          InternetAddress[] cc2 =  InternetAddress.parse(cc);
          msg.setRecipients(Message.RecipientType.CC, cc2);
        }
      }
 
      if(bcc !=null)
      {
        if(!(bcc.trim().equals("")))
        {
          InternetAddress[] bcc2 =  InternetAddress.parse(bcc);
          msg.setRecipients(Message.RecipientType.BCC, bcc2);
        }
      }
 
      // Create subject
      msg.setSubject(subject);
 
      // Create and fill the first message part
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setContent(message, mimeType);
 
      // Create the Multipart and its parts to it
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(mbp1);
 
      // Add the Multipart to the message
      msg.setContent(mp);
Transport transport = 
  session.getTransport("smtp");
transport.connect(
  "smtp.rediffmail.com", "my rediffmail userid", "my rediffmail password");
      Transport.send(msg);     
               
               return true;
    }
    catch (MessagingException ex)
    {
      ex.printStackTrace();
               
               return false;
    }
  }
	}

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

mithunda5011Author Commented:
modified lines are no-18 and no-66

now m getting this error...
--------------------Configuration: <Default>--------------------
javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:319)
    at javax.mail.Service.connect(Service.java:169)
    at javax.mail.Service.connect(Service.java:118)
    at javax.mail.Transport.send0(Transport.java:188)
    at javax.mail.Transport.send(Transport.java:118)
    at a1.sendJavaMail(a1.java:68)
    at a1.main(a1.java:9)
 
Process completed.

Open in new window

Ajay-SinghCommented:
change this

Transport transport =
  session.getTransport("smtp");
transport.connect(
  "smtp.rediffmail.com", "my rediffmail userid", "my rediffmail password");
      Transport.send(msg);  


to

Transport transport =
  session.getTransport("smtp");
transport.connect(
  "smtp.rediffmail.com", "my rediffmail userid", "my rediffmail password");
      transport.sendMessage(msg,to2);
mithunda5011Author Commented:
replaced,but stil same error
Ajay-SinghCommented:
do you have access to rediff pop/smtp? are you able to access from outlook?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mithunda5011Author Commented:
hello,i hv done it.......i m using sify  and using a diff code...its fully working....
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import javax.activation.*;
import java.io.*;
 
public class SendMail {
 
 private MimeMessage mimeMsg; 
 
 private Session session;      
 private Properties props;     
 private boolean needAuth = false;  
 
 private String username = "";  
 private String password = "";
 
 private int filesize = 0;  
 private Multipart mp;   
 
 
/**
 * 
 */
 public SendMail() {
   setSmtpHost("asmtp.sifybroadband.com");
   createMimeMessage();
 }
 
 public SendMail(String smtp){
   setSmtpHost(smtp);
   createMimeMessage();
 }
 
 
/**
 * @param hostName String
 */
 public void setSmtpHost(String hostName) {
   System.out.println("Set system properties&#65306;mail.smtp.host = "+hostName);
   if(props == null)props = System.getProperties();         
props.put("mail.smtp.port", "587");
   props.put("smtp.sifybroadband.com",hostName);                
 }
 
 
/**
 * @return boolean
 */
 public boolean createMimeMessage()
 {
   try{
     System.out.println("Create Mail Session Object&#65281;");
     session = Session.getDefaultInstance(props,null);   
   }
   catch(Exception e){
     System.err.println("Error when create Mail Session Object&#65281;"+e);
     return false;
   }
 
   System.out.println("Creating MIME Mail Object&#65281;");
   try{
     mimeMsg = new MimeMessage(session);                 
     mp = new MimeMultipart();
 
     return true;
   }
   catch(Exception e){
     System.err.println("Create MIME Mail Object Failed&#65281;"+e);
     return false;
   }
 }
 
 
/**
 * @param need boolean
 */
 public void setNeedAuth(boolean need) {
   System.out.println("Set SMTP AUTH&#65306;mail.smtp.auth = "+need);
   if(props == null)props = System.getProperties();
 
   if(need){
     props.put("mail.smtp.auth","true");
   }else{
     props.put("mail.smtp.auth","false");
   }
 }
 
 
/**
 * @param name String
 * @param pass String
 */
 public void setNamePass(String name,String pass) {
   username = name;
   password = pass;
 }
 
 
/**
 * @param mailSubject String
 * @return boolean
 */
 public boolean setSubject(String mailSubject) {
   System.out.println("Set mail subject&#65281;");
   try{
     mimeMsg.setSubject(mailSubject);
     return true;
   }
   catch(Exception e) {
     System.err.println("Set mail subject error&#65281;");
     return false;
   }
 }
 
 
 
/**
 * @param mailBody String
 */
 public boolean setBody(String mailBody) {
   try{
     BodyPart bp = new MimeBodyPart();
     bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312");
     mp.addBodyPart(bp);
 
     return true;
   }
   catch(Exception e){
     System.err.println("Set mail body error&#65281;"+e);
     return false;
   }
 }
 
 
/**
 * @param name String
 * @param pass String
 */
 public boolean addFileAffix(String filename) {
 
System.out.println("Add accessory&#65306;"+filename);
 
try{
     BodyPart bp = new MimeBodyPart();
     FileDataSource fileds = new FileDataSource(filename);
     bp.setDataHandler(new DataHandler(fileds));
     bp.setFileName(fileds.getName());
     filesize += fileds.getFile().length();
     System.out.println(filesize);
     if (filesize >= 4194304) {
           filesize -= bp.getSize();
           return true;
     }
     mp.addBodyPart(bp);
 
     return true;
   }
   catch(Exception e){
     System.err.println("Error when add accessory&#65306;"+filename+e);
     return false;
   }
 }
 
 
/**
 * @param name String
 * @param pass String
 */
 public boolean setFrom(String from) {
   System.out.println("Set mail from&#65281;");
   try{
     mimeMsg.setFrom(new InternetAddress(from));             
     return true;
   }
   catch(Exception e)
   { return false; }
 }
 
 
/**
 * @param name String
 * @param pass String
 */
 public boolean setTo(String to){
   if(to == null)return false;
 
   try{
     mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
     return true;
   }
   catch(Exception e)
   {   return false;   }
 
 }
 
/**
 * @param name String
 * @param pass String
 */
 public boolean setCopyTo(String copyto)
 {
   if(copyto == null)return false;
   try{
     mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
     return true;
   }
   catch(Exception e)
   { return false; }
 }
 
 
/**
 * @param name String
 * @param pass String
 */
 public boolean sendout()
 {
   try{
     mimeMsg.setContent(mp);
     mimeMsg.saveChanges();
     System.out.println("Sending mail....");
 
     Session mailSession = Session.getInstance(props,null);
     Transport transport = mailSession.getTransport("smtp");
     transport.connect((String)props.get("smtp.sifybroadband.com"),username,password);
     transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
     //transport.send(mimeMsg);
 
     System.out.println("Send mail success&#65281;");
     transport.close();
 
     return true;
   }
   catch(Exception e)
   {
     System.err.println("Send mail failed&#65281;"+e);
     return false;
   }
 }
 
 
 /**
  *  Just do it as this
  */
 public static void main(String[] args) {
 
   String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+
       "<div align=center><a href=http://www.csdn.net> csdn </a></div>";
 
   SendMail themail = new SendMail("asmtp.sifybroadband.com");
   themail.setNeedAuth(true);
 
   if(themail.setSubject("HelloWorld") == false) return;
   if(themail.setBody(mailbody) == false) return;
   if(themail.setTo("mithunda50@rediffmail.com") == false) return;
   if(themail.setFrom("mithunda5011@sifymail.com") == false) return;
   //if(themail.addFileAffix("c:\\boot.ini") == false) return;
    themail.setNamePass("my sify id","my sify pass");
    if(themail.sendout() == false) return;    
 }
}

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Protocols

From novice to tech pro — start learning today.