Click here to Skip to main content
15,860,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import javax.mail.*;
import javax.mail.Message;
import java.util.*;
import javax.mail.internet.*;
 
public class Sendemail {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
            final String username="username";
            final String password="password";
            Properties prop=new Properties();
            prop.put("mail.smtp.auth", "true");
            prop.put("mail.smtp.host", "smtp.gmail.com");
            prop.put("mail.smtp.port", "587");
            prop.put("mail.smtp.starttls.enable", "true");
        
            Session session = Session.getDefaultInstance(prop,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
          try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("myemail@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse("receiveremail@gmail.com"));
			message.setSubject("Testing Subject");
			message.setText("Dear Mail Crawler,sNo spam to my email, please!");
                       
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			e.printStackTrace();
		}
            
    }
}
 

 

Please Help Someone..
    I have Exception On this code
 
javax.mail.MessagingException: 502 5.5.1 Unrecognized command. nw9sm29708787pbb.42
 
	at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:2080)
	at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1910)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:652)
	at javax.mail.Service.connect(Service.java:317)
	at javax.mail.Service.connect(Service.java:176)
	at javax.mail.Service.connect(Service.java:125)
	at javax.mail.Transport.send0(Transport.java:194)
	at javax.mail.Transport.send(Transport.java:124)
	at sendemail.Sendemail.main(Sendemail.java:44)
Posted

1 solution

Hello

About sending email using Gmail account, you can refer the flowing code (using TLS connection):
import java.util.Properties;
 
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;
 
public class SendEmailDemo {
 
	public static void main(String[] args) {
 
		final String username = "username@gmail.com";
		final String password = "password";
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
 
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("from-email@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse("to-email@gmail.com"));
			message.setSubject("Testing Subject");
			message.setText("Dear Mail Crawler,"
				+ "\n\n No spam to my email, please!");
 
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}


Or, if you want to send an Email via Gmail SMTP server using SSL connection, try this:
import java.util.Properties;
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;

public class SendEmailDemo {
	public static void main(String[] args) {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("username",
								"password");
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("from@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse("to@gmail.com"));
			message.setSubject("Testing");
			message.setText("Mail content");

			Transport.send(message);

			System.out.println("Sent");

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}


I've tested both of them. They're ok.
Hope those code snippets can help you.
 
Share this answer
 
Comments
[no name] 3-Jan-13 2:56am    
One more point.
To run the above code snippets as well, you have to "Add build path - in Eclipse" this jar : java-mail-1.4.4.jar, download it at http://www.java2s.com/Code/Jar/j/Downloadjavamail144jar.htm
[no name] 3-Jan-13 6:25am    
You can download my demo which was developed in Eclipse at http://sdrv.ms/VxAqON

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900