Java Mailing Service

Discussion in 'Java' started by adroit89, Mar 10, 2009.

  1. adroit89

    adroit89 New Member

    Joined:
    Jan 5, 2007
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    hi guys...i am presently working on my mini-project in my college...and a module of it requires the use of java mailing service...i need to send a mail using SMTP protocol through my java application...i learnt that gmail can be used as a SMTP server to send mails...this is the code of my application...but when i am running this program i am getting a error stating NO CLASS FOUND javax.mail.Authenticator

    Code:
    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.MimeMessage;  
          
       public class SendEmail
      {  
         public static void main(String args[]) throws Exception
         {
               // Set the host smtp address  
               Properties props = new Properties();  
               props.put("mail.smtp.starttls.enable", "true");  
               props.put("mail.smtp.host", "smtp.gmail.com");  
               props.put("mail.transport.protocol", "smtp");  
               props.put("mail.smtp.auth", "true");  
               Authenticator auth = new   SMTPAuthenticator("MY GMAIL ID","PASSWORD");  
               Session session = Session.getDefaultInstance(props, auth);  
               session.setDebug(false);  
                // create a message  
               Message msg = new MimeMessage(session);  
                // set the from and to address  
               InternetAddress addressFrom = new InternetAddress("praveen");  
               msg.setFrom(addressFrom);  
               InternetAddress addressTo = new InternetAddress("adroit89@yahoo.co.in");  
               msg.setRecipient(Message.RecipientType.TO, addressTo);  
               // Setting the Subject and Content Type  
               msg.setSubject("try");  
               msg.setContent("hiiii", "text/html");  
               Transport.send(msg);  
           }  
        /** 
        * SimpleAuthenticator is used to do simple authentication when the SMTP 
        * server requires it. 
        */  
             
         public static class SMTPAuthenticator extends javax.mail.Authenticator 
        {  
               private String username;  
               private String password;  
               public SMTPAuthenticator(String username, String password) 
              {  
                   this.username = username;  
                   this.password = password;  
               }  
               public PasswordAuthentication getPasswordAuthentication() 
              {  
                   return new PasswordAuthentication(username, password);  
              }  
        }        
    }
     
    Last edited by a moderator: Mar 10, 2009

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice