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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
04-04-2006, 01:54 AM
#1
LQ Newbie
Registered: Feb 2004
Distribution: Suse, LFS(Linux From Scratch)
Posts: 20
Rep:
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");
}
}
04-04-2006, 02:18 AM
#2
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165
Quote:
Originally Posted by keikun_naruchan
This is the program. any suggestions are appreciated.
Sure, but why not telling us what is going wrong ?
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 02:34 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News