Friday, May 11, 2012

Send Mail With Java


This is a sample code for writing with java to send email:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class test {
    public static void main(String args[]) throws Exception {
        String user = "test";
        String password = "test";
        String fromAddress = "test@localhost";
        String toAddress = "test@gmail.com";    
        Properties properties = new Properties();  
        properties.put("mail.smtp.host", "localhost");  
        properties.put("mail.smtp.port", "25");  
        properties.put("mail.smtp.username", user);  
        properties.put("mail.smtp.password", password);
        properties.put("mail.transport.protocol", "smtp");
        Session session = Session.getDefaultInstance(properties, null);  
        try          
        {      
            Message message = new MimeMessage(session);      
            message.setFrom(new InternetAddress(fromAddress));      
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));      
            message.setSubject("Email from our JAMEs");      
            message.setText("hiiiiii!!");      
            Transport.send(message);      
            System.out.println("Email sent");  
        }  
        catch (MessagingException e)  
        {    
            e.printStackTrace();  
        }
        }
}

Solution:
If we test it and it cannot send. This is the guide for resolving problem as below:
1.Go to the path : \apps\james\conf
2.Open for edit the filename 'james-fetchmail.xml'
3.Change "fetchmail" from false to true as:
4.Restart the Apache James Server.
5.You would see the following messages printed on the console after you start the server:
James Mail Server 2.3.2 Remote Manager Service started plain:4555 POP3 Service started
plain:110 SMTP Service started plain:25 NNTP Service started plain:119 FetchMail Started.

Thursday, May 10, 2012

Using Array String in Java

This is simple code for using array string:


How to create and use Nested and Inner Class in Java

- ជាដំបូងយើងត្រូវបង្កើត file  class name: outer.java:

public class Outer {
    int outer_x=100;
   
    void test(){
        Inner inner=new Inner();  
        inner.display();
    }
    class Inner{
        void display(){
            System.out.println("display outer_x:"+outer_x);
        }
    }
}

ហើយបន្ទាប់មកបង្កើត File main class name: Callouter.java:

public class CallOuter {
    public static void main(String args[]){
        Outer obj=new Outer();
        obj.test();
    }
}

វាកូដគំរូដ៏សាមញ្ញមួយ សូមអរគុណ!

Wednesday, May 9, 2012

Sample Some configure in Linux


I. Samba Server (text mod):
            - We have to have three packages: samba-common-<version>.rpm, samba-<version>.rpm, system-config-samba<version>.rpm
            - We have to check samba packages: # rpm –qa samba samba-common system-config-samba if it doesn’t have, we will install it.
            - Create directory: # mkdir /var/Docs
            - Create Users:
            # useradd dara, # passwd 123456
            # useradd vanna, # passwd 123456
            - # chmod –R 444 /var/Docs // note: we don’t set permission on it
            - adding samba users:
            # smbpasswd –a dara , password:123456
# smbpasswd –a vanna, password:123456
- adding sharing
# vi /etc/samba/smb.conf
[Shared]
            Path = /var/Docs/
            Browseable = yes
            Read only = yes
            Valid user = dara
[Shared]
            Path = /var/Docs
            Browseable = yes
            Writeable = yes
            Valid user = vanna
- Close firewall and selinux:
# system-config-firewall (Press Disable à OK)
# system-config-selinx (Choose Disable, and permissive)     
- # service smb restart
- Testing shared folder from windows (http://localhost:901/)
II. Samba Swat (Interface):
            - We have to have two packages: samba-swat-<version>.rpm, xinetd-<version>.rpm
            - We have to check samba packages: # rpm –qa samba-swat xinetd  if it doesn’t have, we will install it.
            - Edit /etc/xinetd.d/swat
            # vi /etc/xinetd.d/swat
            Change “disable=yes” to “disable=no”
            - Open browser logon as root (http://localhost:901/)

III. How to configure IP address:
            - # ifconfig –a
            - # vi /etc/sysconfig/network
            SET: “HOSTNAME=setec.abc.com”
            - # vi /etc/sysconfig/network-script/ifcfg-eth0
            Set: BOOTPROTO=none, IPADDR=192.168.10.1, NETMASK=255.255.255.0
            - # service network restart
            - # ifconfig –a or # ifconfig eth0
IV. How to configure DNS Server:
            +  check service bind
            - # rpm –qa bind bind-utils bind-libs bind-chroot // if not, we install it from Disk
            - # vi /var/named/chroot/etc/named.conf
            Add:
            . forward zone:
Zone “abc.com” IN {
            Type master;
            File “abc.f”;
};
. reverse zone:
Zone “10.168.192.in-addr.arpa” IN {
            Type master;
            File “abc.r”;
};
- # vi /var/named/chroot/var/named/abc.f
Add:
$TTL 2D
@ SOA           dns.abc.com.   root.abc.com. (1 2D 1D 1W 1H)
                        NS       dns.abc.com.
                        A         192.168.10.1
Dns                  A         192.168.10.1
- # vi /var/named/chroot/var/named/abc.r
Add:
$TTL 2D
@ SOA           dns.abc.com.   root.abc.com.  (1 2D 1D 1W 1H)
                        NS       dns.abc.com.
                        A         192.168.10.1
1                      PTR     dns.abc.com.
- # service named restart
- # vi /etc/resolve.conf
Add # before line as below:
# search com
            # nameserver 192.168.10.1
            # nameserver 192.168.10.2
- dig abc.com or nslookup abc.com
V. How to configure Web Server, FTP Server:
            - first, we have configure DNS Server:
            -# vi /var/named/chroot/var/named.conf
            Add:
                        Zone    “abc.com” IN {
            Type master;
            File “abc”;
};
            - # vi /var/named/chroot/var/named/abc
            Add:
            $TTL 2D
            @ SOA           dns.abc.com.   root.abc.com. (1 2D 1D 1W 1H)
                                    NS       dns.abc.com.
                                    A         192.168.10.1
            Dns                  A         192.168.10.1
            www               CNAME         dns
            ftp                   CNAME         dns
            - # service named restart
            - Check web server package:
            - # rpm –qa httpd // if not, we install it from CD
            - Create Directory for Web
            # mkdir /var/abc
            - # chmod –R 755 /var/abc
            - # vi /var/abc/index.html
            - # vi /etc/httpd/conf/httpd.conf
            Add:
            NameVirtualHost        192.168.10.1
            <VirtualHost 192.168.10.1>
                        DocumentRoot  /var/abc/
                        ServerName    abc.com          
                        DirectoryIndex           index.html
            </VirtualHost>
            - Close firewall and selinux service:
            # system-config-firewall
            # system-config-selinux
            - # service httpd restart
            + Check ftp package:
            # rpm –qa vsftpd  // if not, we install it from CD
            - Create directory for ftp server:
            # mkdir /var/ftp
            # chmod –R 755 /var/ftp
            # useradd –d /var/ftp  webadmin
            # passwd webadmin
            - # vi /etc/vsftpd/vsftpd.conf
            Change “anonymous_enable=YES” to “anonymous_enable=NO”
            - # service vsfptd restart

VI. How to configure DHCP Server:
            - check dhcp package:
            # rpm –qa dhcpd // if not, we install it from CD
            - # vi /etc/sysconfig/network-scripts/ifcfg-eth0
            BOOTPROTO=static
            ONBOOT=yes
            IPADDR=192.168.10.1
            NETMASK=255.255.255.0
            NETWORK=192.168.10.0
            - # service network  restart
            - # ifconfig eth1
            - # vi /etc/dhcpd.conf
            Add:
            ddns-update-style interim;
            ignore client-updates;
            subnet 192.168.10.0 netmask 255.255.255.0 {
            option routers                          192.168.10.1;
            option subnet-mask                 255.255.255.0;
            range   192.168.10.50 192.168.10.100;
            default-lease-time       21600;
            max-lease-time            43200;
            host PC-20001 {
            hardware ethernet 00:0c:29:58:6e:36;
            fixed-address 192.168.10.91;
}
}

- # service dhcpd restart
- # chkconfig --list dhcpd
- # chkconfig --level 35 dhcpd on
- # chkconfig --list dhcpd
- # reboot
- # more /var/lib/dhcpd/dhpcd.leases

VII. How to configure NFS server:
            - We have to have two files: nfs-utils-<version>.rpm, system-config-utils-<version>.rpm
            - Check NFS packages: # rpm –qa nfs-utils system-config-nfs // if not, we install it from CD
            -  Create folder for sharing
            # mkdir /tmp/Shared
            - # vi /etc/exports
            Add:
            /tmp/Shared     192.168.10.1/255.255.255.0 (sync,rw) //client can read and write on network
            Or add
            /tmp/Shared     192.168.10.1/255.255.255.0 (sync,rw,no_root_squash)
            Or add
/tmp/Shared     192.168.10.1/255.255.255.0 (sync,ro,no_root_squash)
- # exportfs –a
            - # service nfs restart
            **mount sharing for client:
            # mount 192.168.9.1:/shared   /tmp/Shared
            *** Create automount sharing for client:
            # vi /etc/fstab
            Add
            192.168.100.1:/shared    /tmp/Shared   nfs   defaults   1  2
VIII. How to configure Mail Server:
            We have to install DNS server:
- # vi /var/named/chroot/etc/maned.conf
            Add:
            Zone “abc.com” IN {
            Type master;
            File “abc”;
};
- # vi /var/named/chroot/var/maned/abc
Add:
$TTL 2D
@ SOA           dns.abc.com.   root.abc.com. (1 2D 1D 1W 1H)
                        NS       dns.abc.com.
                        Mx       10        mail.abc.com.
A         192.168.10.1
Dns                  A         192.168.10.1
Mail                 A         192.168.10.1
- # service named restart
            - We have to have three packages: sendmail-<version>.rpm, sendmail-cf-<version>.rpm, sendmail-doc-<version>.rpm
            - Check mail packages:
            # rpm –qa sendmail, # rpm –qa sendmail-cf, # rpm –qa sendmail-doc // if not we install it
- # vi /etc/mail/sendmail.mc
Delete one line:
dnl # DAEMON_OPTIONS(“Port=smtp, Addr=127.0.0.1, Name=MTA”) dnl
            - #m4 /etc/mail/sendmail.mc > /etc/mail/sendmail-cf
            - # service sendmail restart
           
IX. How to configure DHCP Client:

- /etc/sysconfig/network file should contain the following line:

NETWORKING=yes

The /etc/sysconfig/network-scripts/ifcfg-eth0 file should contain the following lines:

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

A configuration file is needed for each device to be configured to use DHCP.

Other options
PEERDNS=, where is one of the following:
yes — Modify /etc/resolv.conf with information from the server. If using DHCP, then yes is the default.
no — Do not modify /etc/resolv.conf.

USERCTL=, where is one of the following:
yes — Non-root users are allowed to control this device.
no — Non-root users are not allowed to control this device.


Thursday, May 3, 2012

Removing an element from an Array (Java)




public static String[] removeElements(String[] input, String deleteMe) {
    List result = new LinkedList();

    for(String item : input)
        if(!deleteMe.equals(item))
            result.add(item);

    return result.toArray(input);
}