LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-04-2006, 01:54 AM   #1
keikun_naruchan
LQ Newbie
 
Registered: Feb 2004
Distribution: Suse, LFS(Linux From Scratch)
Posts: 20

Rep: Reputation: 0
Question 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");
    }
}
 
Old 04-04-2006, 02:18 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,506

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by keikun_naruchan
This is the program. any suggestions are appreciated.
Sure, but why not telling us what is going wrong ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Mail fails to send a HTML mail eantoranz Programming 1 11-10-2004 01:47 PM
Server does'nt send mail until shutdown and LAN can't send or receive. Wolfy Linux - Networking 0 08-02-2004 07:31 PM
spammer using my mail server send spam email pd5c18 Linux - Networking 2 07-14-2004 01:32 AM
Configuring SEnd mail as Email server in Redhat Linux 8.0 ganeshrk Linux - Networking 0 01-31-2004 04:02 AM
using `mail` to send email to outside world ganninu Linux - Networking 0 08-04-2003 04:35 AM


All times are GMT -5. The time now is 07:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration