Hi ..
I am new to JavaMail...

http://209.85.175.104/search?q=cache:h2HRteCAXKoJ:www.zukowski.net/WebTech-JavaMail.ppt+javamail+faq&hl=en&ct=clnk&cd=4&gl=in
i am trying to run this code ...from this url..

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*; 
public class MailExample
{
	public static void main (String args[]) throws Exception 
	{
		String host = args[0];
	    String from = args[1];
	    String to = args[2]; 
	    // Get system properties
	    Properties props = System.getProperties(); 
	    // Setup mail server
	    props.put("mail.smtp.host",host );
		
		// Get session
	    Session session = Session.getInstance(props, null); 
	    // Define message
	    MimeMessage message = new MimeMessage(session);

	    message.setFrom(new InternetAddress(from));
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
	    message.setSubject("Hello JavaMail");
	    message.setText("Welcome to JavaMail"); 
	    // Send message
	    Transport.send(message);

  }

}

It gives Error..
C:\Program Files\Java\jdk1.6.0_02\bin>java MailExample smtp.gmail.com xyz@gmail.
com pqr@gmail.com
Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0
Must issue a STARTTLS command first. 28sm12530978wfd.4

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1
515)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
at MailExample.main(MailExample.java:26)

How to run this??
plz help me??

Plz give me simple example that i can send email ... through smtp server..

public class SendMailUsingAuthentication
{
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  private static final String SMTP_AUTH_USER = "vpatil";
  private static final String SMTP_AUTH_PWD  = "qaz123qaz";
  private static final int SMTP_PORT  = 465;

  private static final String emailMsgTxt      = "Online Order Confirmation Message. Also include the Tracking Number.";
  private static final String emailSubjectTxt  = "Order Confirmation Subject";
  private static final String emailFromAddress = "vpatil@gmail.com";

  // Add List of Email address to who email needs to be sent to
  private static final String[] emailList = {"pp2007@gmail.com"};

  public static void main(String args[]) throws Exception
  {
    SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
    smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);

  }

  public void postMail( String recipients[ ], String subject,
                            String message , String from) 
  {
      try{
    boolean debug = false;

     //Set the host smtp address
//  Set the host smtp address

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.port", SMTP_PORT);
    props.put("mail.smtp.starttls.enable","true");

    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true");
     props.put("mail.smtp.socketFactory.port", SMTP_PORT);
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
     props.put("mail.smtp.socketFactory.fallback", "false");

     SecurityManager security = System.getSecurityManager();


    Authenticator auth = new SMTPAuthenticator();
    Session session = Session.getInstance(props, auth);
    session.setDebug(debug);

    // create a message

    Message msg = new MimeMessage(session);

    // set the from and to address

    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Setting the Subject and Content Type


    msg.setText(message);
    msg.setSubject(subject);
     msg.setContent(message, "text/plain");

    Transport.send(msg);






      }catch (MessagingException e) {
        // TODO: handle exception
          e.printStackTrace();
    }
 }


/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{

    public PasswordAuthentication getPasswordAuthentication()
    {
          try{
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        System.out.println("username  "+ username+"     Password "+password);
        return new PasswordAuthentication(username, password);
          }catch (Exception e) {

        }
          return null;
    }
}

}

When i m trying to run....

C:\Program Files\Java\jdk1.6.0_02\bin>java SendMailUsingAuthentication
username vpatil Password qaz123qaz

javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connecti
on?
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java
:1611)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:41
2)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java
:109)
at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:56)

Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext conne
ction?
at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(InputRec
ord.java:523)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:355)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.j
ava:789)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SS
LSocketImpl.java:1096)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketIm
pl.java:744)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:
75)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:88)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java
:1589)
... 9 more

How to execute this code..plz help ??

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
import com.sun.mail.smtp.SMTPSSLTransport;
 
/*
  To use this program, change values for the following three constants,

    SMTP_HOST_NAME -- Has your SMTP Host Name
    SMTP_AUTH_USER -- Has your SMTP Authentication UserName
    SMTP_AUTH_PWD  -- Has your SMTP Authentication Password

  Next change values for fields

  emailMsgTxt  -- Message Text for the Email
  emailSubjectTxt  -- Subject for email
  emailFromAddress -- Email Address whose name will appears as "from" address

  Next change value for "emailList".
  This String array has List of all Email Addresses to Email Email needs to be sent to.
  Next to run the program, execute it as follows,
  SendMailUsingAuthentication authProg = new SendMailUsingAuthentication();

*/
 
public class SendMailUsingAuthentication
{
 
//private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.com";
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  private static final String SMTP_AUTH_USER = "vpatil";
  private static final String SMTP_AUTH_PWD  = "qaz123qaz";
  private static final int SMTP_PORT  = 465;
 
  private static final String emailMsgTxt      = "Online Order Confirmation Message. Also include the Tracking Number.";
  private static final String emailSubjectTxt  = "Order Confirmation Subject";
  private static final String emailFromAddress = "vpatil@gmail.com";
 
  // Add List of Email address to who email needs to be sent to
  private static final String[] emailList = {"pp2007@gmail.com"};
 
  public static void main(String args[]) throws Exception
  {
    SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
    smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
    
  }
 
  public void postMail( String recipients[ ], String subject,
                            String message , String from) 
  {
	  try{
    boolean debug = false;
 
     //Set the host smtp address
//  Set the host smtp address
    
	Properties props = new Properties();
	props.put("mail.transport.protocol", "smtp");
	props.put("mail.smtp.port", SMTP_PORT);
	props.put("mail.smtp.starttls.enable","true");
	
	props.put("mail.smtp.host", SMTP_HOST_NAME);
	props.put("mail.smtp.auth", "true");
	 props.put("mail.smtp.socketFactory.port", SMTP_PORT);
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	 props.put("mail.smtp.socketFactory.fallback", "false");
 
	 SecurityManager security = System.getSecurityManager();
	
	
	Authenticator auth = new SMTPAuthenticator();
	Session session = Session.getInstance(props, auth);
	session.setDebug(debug);
 
	// create a message
	
	Message msg = new MimeMessage(session);
 
	// set the from and to address
	
	InternetAddress addressFrom = new InternetAddress(from);
	msg.setFrom(addressFrom);
 
	InternetAddress[] addressTo = new InternetAddress[recipients.length];
	for (int i = 0; i < recipients.length; i++) {
		addressTo[i] = new InternetAddress(recipients[i]);
	}
	msg.setRecipients(Message.RecipientType.TO, addressTo);
 
	// Setting the Subject and Content Type
	
	
	msg.setText(message);
	msg.setSubject(subject);
	 msg.setContent(message, "text/plain");
	
	Transport.send(msg);
	
	  }catch (MessagingException e) {
		// TODO: handle exception
		  e.printStackTrace();
	}
 }
 
 
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
 
    public PasswordAuthentication getPasswordAuthentication()
    {
    	  try{
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        System.out.println("username  "+ username+"     Password "+password);
        return new PasswordAuthentication(username, password);
    	  }catch (Exception e) {
			
		}
    	  return null;
    }
}
 
}

Spamming your own thread won't get you help any sooner - just the opposite I would wager. Also, "plz" is not a word. Read the forum rules regarding use of proper English. This is not a chat room.

and your posts just show you haven't got a clue as to what you're doing. That's not indicative of someone who actually does some research beyond copying code samples and hoping they will do what he wants.

Here is what I use:

public final class Mail {
	
	public static final void sendMessage(String mailAddress, String text,
			String subject) throws Exception
	{

	 Properties myProperties=new Properties();
           
        myProperties.put("mail.smtp.host", someHost);
        myProperties.put("mail.smtp.auth", "true");
        myProperties.put("mail.debug", "false");
        myProperties.put("mail.smtp.starttls.enable", "true");
       
        LmsAuthenticator ax = new LmsAuthenticator(address, pass);
		Session ses = Session.getInstance(myProperties,ax);
	
		Message msg=new MimeMessage(ses);
		
        InternetAddress fromAddress = new InternetAddress(address,yourNameForInstance);
        InternetAddress toAddress = new InternetAddress(mailAddress);
			
        msg.setFrom(fromAddress);
		msg.setRecipient(Message.RecipientType.TO,toAddress);

		msg.setSubject(subject);
		String body = text;
		msg.setContent(body, "text/plain");
			
		msg.setSentDate(new Date());
		Transport trans = ses.getTransport("smtp");
        trans.connect(host,"","");
		msg.saveChanges();
		com.sun.mail.smtp.SMTPSSLTransport.send(msg);
		trans.close();
	}
}

And the authenticator:

package ro.fortech.lms.messaging;

import javax.mail.*;

class LmsAuthenticator extends Authenticator
{  
	private String addr=null, pw=null;
	
	public LmsAuthenticator(String addr, String pw) { 
		super(); 
		this.addr = addr;
		this.pw = pw;
	}

	protected PasswordAuthentication getPasswordAuthentication() { 
		return new PasswordAuthentication(addr,pw); 
		}
	}

Well, this approach needs valid username and password of an e-mail box. However, for simple applications it's suitable.

This article has been dead for over six months. Start a new discussion instead.