Click here to Skip to main content
11,705,763 members (57,389 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
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 2-Jan-13 18:58pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 218
1 Sergey Alexandrovich Kryukov 170
2 Inimicos 60
3 ProgramFOX 50
4 Richard MacCutchan 45
0 OriginalGriff 9,037
1 Sergey Alexandrovich Kryukov 8,417
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100