This discussion is archived
1 Reply Latest reply: Apr 19, 2007 1:52 PM by 807596 RSS

New to java mail please help ..

807596 Newbie
Currently Being Moderated
Hi all

iam new to java mail technology.
Here is the class that i have used to send mail.My requirement is whenever user completes a registration , a confirmation mail is tyo be send to him automatically.

How can i identify mail server and its SMTP address? Please help.

sendmail.sendEmail(); This is the method i have called in the servlet.






mail class


package org.kran.hris.domain;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author kran
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class sendMail {

          
     public void sendEmail()
     {
          System.out.println("Inside mail class");
          String host="smtp.mail.yahoo.com";
          String from="jakri_007@yahoo.com";
          String to="jkrishnanj@gmail.com";
          String username="linukumar";
          String password="password";
          Properties props = new Properties();
          props.put("mail.smtp.host",host);
          props.put("mail.smtp.user",username);
          props.put("mail.smtp.password",password);
          props.put("mail.smtp.auth","true");
          props.put("mail.debug","true");
          Session session = Session.getInstance(props);
          
          try{
               System.out.println("Inside mail class2");
          Message msg=new MimeMessage(session);
          msg.setFrom(new InternetAddress(from));
          InternetAddress fromaddr = new InternetAddress(to);
          msg.setRecipient(Message.RecipientType.TO,fromaddr);
          msg.setSubject("Test email through JAVA");
          msg.setSentDate(new Date());
          System.out.println("Inside mail class3");
          msg.setText("Test email for HRIS application from java application using java mail");
          Transport bus=session.getTransport("smtp");
          bus.connect(host,username,password);
          msg.saveChanges();
          Transport.send(msg);
          bus.close();
          System.out.println("Inside mail class4");
          }
          catch (MessagingException msex)
          {
               msex.printStackTrace();
          }
     }
     
     
     
}


Any body please help........