JAVA SWING EMAIL SENDING ...PLS HELP ME

danlix

Well-known member
  • Feb 12, 2012
    2,814
    767
    113
    Dark Horizon
    Yaluwane,

    mata time diffrence eka calculate karanna godak yaluwo help karane.
    E wede goda. :) thanks all

    But now

    mata swing walin email ekak yawagannna oni den.

    Mata mekata help karanna. please ..

    Thanks
     

    NO_MeRcY

    Well-known member
  • Jun 14, 2010
    5,423
    449
    83
    Singapore
    without GUI

    Code:
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.Message.RecipientType;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    /**
     *
     * @author no_mercy
     */
    public class sendMail {
    
        private String from;
        private String to;
        private String subject;
    
        public String getFrom() {
            return from;
        }
    
        public void setFrom(String from) {
            this.from = from;
        }
    
        public String getSubject() {
            return subject;
        }
    
        public void setSubject(String subject) {
            this.subject = subject;
        }
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        public String getTo() {
            return to;
        }
    
        public void setTo(String to) {
            this.to = to;
        }
        private String text;
    
        public sendMail(String from, String to, String subject, String text) {
            this.from = from;
            this.to = to;
            this.subject = subject;
            this.text = text;
        }
    
        public void send() {
    
            Properties props = new Properties();
            props.put("mail.smtp.host", "192.168.xxx.xxx");
            props.put("mail.protocol.port", 25);
    
            Session mailSession = Session.getDefaultInstance(props);
            Message simpleMessage = new MimeMessage(mailSession);
    
            InternetAddress fromAddress = null;
            InternetAddress toAddress = null;
            try {
                fromAddress = new InternetAddress(from);
                toAddress = new InternetAddress(to);
            } catch (AddressException e) {
                e.printStackTrace();
            }
    
            try {
                simpleMessage.setFrom(fromAddress);
                simpleMessage.setRecipient(RecipientType.TO, toAddress);
                simpleMessage.setSubject(subject);
                simpleMessage.setText(text);
    
                Transport.send(simpleMessage);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }


    test client (GUI eka hada ganna)

    Code:
    import email.sendMail;
    
    /**
     *
     * @author no_mercy
     */
    public class TestClient {
    
        public static void main(String args[]) {
    
            String from = "[email protected]";
            String to = "[email protected]";
            String subject = "Test";
            String message = "A test message";
    
            sendMail sendMail = new sendMail(from, to, subject, message);
            sendMail.send();
            System.out.println("Email Sent");
    
        }
    }


    me tika run karala balapan. ubata therewi karnna one dey
     

    danlix

    Well-known member
  • Feb 12, 2012
    2,814
    767
    113
    Dark Horizon
    without GUI

    Code:
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.Message.RecipientType;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    /**
     *
     * @author no_mercy
     */
    public class sendMail {
    
        private String from;
        private String to;
        private String subject;
    
        public String getFrom() {
            return from;
        }
    
        public void setFrom(String from) {
            this.from = from;
        }
    
        public String getSubject() {
            return subject;
        }
    
        public void setSubject(String subject) {
            this.subject = subject;
        }
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        public String getTo() {
            return to;
        }
    
        public void setTo(String to) {
            this.to = to;
        }
        private String text;
    
        public sendMail(String from, String to, String subject, String text) {
            this.from = from;
            this.to = to;
            this.subject = subject;
            this.text = text;
        }
    
        public void send() {
    
            Properties props = new Properties();
            props.put("mail.smtp.host", "192.168.xxx.xxx");
            props.put("mail.protocol.port", 25);
    
            Session mailSession = Session.getDefaultInstance(props);
            Message simpleMessage = new MimeMessage(mailSession);
    
            InternetAddress fromAddress = null;
            InternetAddress toAddress = null;
            try {
                fromAddress = new InternetAddress(from);
                toAddress = new InternetAddress(to);
            } catch (AddressException e) {
                e.printStackTrace();
            }
    
            try {
                simpleMessage.setFrom(fromAddress);
                simpleMessage.setRecipient(RecipientType.TO, toAddress);
                simpleMessage.setSubject(subject);
                simpleMessage.setText(text);
    
                Transport.send(simpleMessage);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }


    test client (GUI eka hada ganna)

    Code:
    import email.sendMail;
    
    /**
     *
     * @author no_mercy
     */
    public class TestClient {
    
        public static void main(String args[]) {
    
            String from = "[email protected]";
            String to = "[email protected]";
            String subject = "Test";
            String message = "A test message";
    
            sendMail sendMail = new sendMail(from, to, subject, message);
            sendMail.send();
            System.out.println("Email Sent");
    
        }
    }


    me tika run karala balapan. ubata therewi karnna one dey

    Ok. thanks. :)