LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   need help JAVA send email to any mail server, yahoo, google.. etc... (http://www.linuxquestions.org/questions/programming-9/need-help-java-send-email-to-any-mail-server-yahoo-google-etc-431562/)

keikun_naruchan 04-04-2006 01:54 AM

need help JAVA send email to any mail server, yahoo, google.. etc...
 
This is the program. any suggestions are appreciated.

Code:


import java.util.*;
import java.sql.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;


public class Notifier //throws MessagingException
{
    public static void main(String args[]) throws Exception
    {
        NotifierThread NThread = new NotifierThread();
               
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
       
        EmailThread EThread = new EmailThread();
       
        //String[] arrayKo = { "kikoyz_it@yahoo.com","def","xyz" };
        String[] arrayKo = { "keikun_naruchan@yahoo.com","def","xyz" };
       
        try
        {
            //              ( String recipients[ ], String subject, String message , String from)
            EThread.postMail(arrayKo , "NOTIFY", "Contract Overdue" , "johannazanza@yahoo.com");
            System.out.println("ethread");
        }
        catch(MessagingException me)
        {
            me.printStackTrace();
        }
           
        //Thread t = new Thread(NThread);
        //t.start(); 
       
     
    } 
}

       
     

class NotifierThread implements Runnable

    public void run()
    {     
        while (true)
        {
            Calendar cal = new GregorianCalendar();           
            int hour12 = cal.get(Calendar.HOUR);            // Range 0..11
            //int hour24 = cal.get(Calendar.HOUR_OF_DAY);    // Range 0..23
            int min = cal.get(Calendar.MINUTE);            // Range 0..59
            int sec = cal.get(Calendar.SECOND);            // Range 0..59
            //int ms = cal.get(Calendar.MILLISECOND);        // Range 0..999
            int ampm = cal.get(Calendar.AM_PM);            // Range 0=AM, 1=PM
            String am_pm = "";
                     
            if(ampm == 0)
            {
                am_pm = "AM";
            }
            else
            {
                am_pm = "PM";
            }
           
            System.out.println("Time " + hour12 + ":" + min + ":" + sec + " " + am_pm);
           
            if(sec == 10)
            {
                System.out.println("YIPEE");
            }
           
            try
            {
                Thread.sleep(1000);
            }
            catch(Exception e){}
        }
    }
}



class EmailThread //throws MessagingException
{
    public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
    {
        boolean debug = false;

        //Set the host smtp address
        Properties props = new Properties();
        //props.put("mail.philweb.com", "25");
        props.put("localhost", "25");
        //props.put("mail.smtp.host", "smtp.jcom.net");

        // create some properties and get the default Session
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);   

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)
        {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);


        // Optional : You can also set your custom headers in the Email if you Want
        msg.addHeader("MyHeaderName", "myHeaderValue");

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
        System.out.println("postmail");
    }
}


jlliagre 04-04-2006 02:18 AM

Quote:

Originally Posted by keikun_naruchan
This is the program. any suggestions are appreciated.

Sure, but why not telling us what is going wrong ?


All times are GMT -5. The time now is 05:34 PM.