Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: sendEmail

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default sendEmail

    hi guys
    I write follow code to send email from my application, but this code don't work
    please help me


    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.swing.JOptionPane;
    import javax.activation.*;
     
    public class SendEmail {
     
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String to="raziyeh_hematpoor@yahoo.com";
    		String from="r.hematpoor@gmail.com";
    		String host="localhost";
    		Properties properties=System.getProperties();
    		properties.setProperty("mail.smtp.host", host);
    		Session session=Session.getDefaultInstance(properties);
     
    		try{
    			MimeMessage message=new MimeMessage(session);
    			message.setFrom(new InternetAddress(from));
    			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    			message.setSubject("this is a task");
    			message.setText("description!!!!!!!!!!");
    			Transport.send(message);
    			JOptionPane.showMessageDialog(null, "send message successfully....");
    		}
    		catch(MessagingException mex) {
    			mex.printStackTrace();
    		}
     
    	}
     
    }
    Last edited by jps; August 25th, 2013 at 02:53 PM. Reason: code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: sendEmail

    Quote Originally Posted by kaviri View Post
    but this code don't work
    Hi kaviri. This doesn't tell us much. What does the code do? Is there an error? Does it give incorrect output? Perhaps nothing happens at all.
    Try to explain what happens vs what you expected to happen. Post the full text of any error messages.
    Please use code tags when posting code. Assistance can be found here.

  3. The Following User Says Thank You to jps For This Useful Post:

    kaviri (August 26th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: sendEmail

    The code sends this message to my localhost server:
    From: r.hematpoor@gmail.com
    To: raziyeh_hematpoor@yahoo.com
    Message-ID: <24395706.0.1377461782631.JavaMail.Norm@Norms-Netbook>
    Subject: this is a task
    MIME-Version: 1.0
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit

    description!!!!!!!!!!
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    kaviri (August 26th, 2013)

  6. #4
    Member
    Join Date
    Sep 2012
    Posts
    30
    My Mood
    Cool
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: sendEmail

    Hi,,

    You need set object properties. Take a look these code below

    import java.util.Properties;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
     
    /**
     *
     * @author user
     */
    public class SendEmail {
     
        public static void main(String[] args) {
            // Recipient's email ID needs to be mentioned.
            String to = "userTo@gmail.com";
     
            // Sender's email ID needs to be mentioned
            String from = "userFrom@gmail.com";
     
            // Assuming you are sending email from localhost
            String host = "gmail.com";
     
            // Get system properties
    //        Properties properties = System.getProperties();
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.debug", "false");
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.port", "465");
            // Setup mail server
    //        properties.setProperty("smtp.gmail.com", host);
     
            // Get the default Session object.
            Session session = Session.getDefaultInstance(properties, new Authenticator() {@Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("userFrom@gmail.com", "YourPassword");
            }});
     
            try {
                // Create a default MimeMessage object.
                MimeMessage message = new MimeMessage(session);
     
                // Set From: header field of the header.
                message.setFrom(new InternetAddress(from));
     
                // Set To: header field of the header.
                message.addRecipient(Message.RecipientType.TO,
                        new InternetAddress(to));
     
                // Set Subject: header field
                message.setSubject("This is the Subject Line!");
     
                // Now set the actual message
                message.setText("This is actual message");
     
                // Send message
                Transport.send(message);
                System.out.println("Sent message successfully....");
            } catch (MessagingException mex) {
                mex.printStackTrace();
            }
        }
    }