Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

843830Apr 16 2005 — edited Apr 8 2009
Hello, I was trying to send mails via GMail's smtp server (smtp.gmail.com) but the following exception occurred. I used port 25 (used 467 also, didnt work). Would anybody tell what the following exception mean. Thanx.
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
Here's my code:
import javax.mail.*;
import javax.mail.event.TransportListener;
import javax.mail.event.TransportEvent;
import javax.mail.internet.*;
import java.util.Properties;
import javax.activation.*;

class MailSender {
	private String mailHost="smtp.gmail.com";
	private String body;
	private String myFile="F:\\DRacing.avi";

	private Properties props;
	private Session mailSession;
	

	private MimeMessage message;
	private InternetAddress sender;

	private Multipart mailBody;
	private MimeBodyPart mainBody;
	private MimeBodyPart mimeAttach;

	private DataSource fds;


	MailSender()
	{
		//Creating a Session
		props=new Properties();
				props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.host", mailHost);
		props.put("mail.smtp.port", "25");
		props.put("mail.smtp.auth", "true");

		mailSession=Session.getDefaultInstance(props, new MyAuthenticator());

		
		//Constructing and Sending a Message
		try
		{
			//Starting a new Message
			message=new MimeMessage(mailSession);
			mailBody=new MimeMultipart();

			//Setting the Sender and Recipients
			sender=new InternetAddress("dider7@gmail.com", "Kayes");
			message.setFrom(sender);

			InternetAddress[] toList={new InternetAddress("thegreendove@yahoo.com")};
			

			message.setRecipients(Message.RecipientType.TO, toList);
			
			//Setting the Subject and Headers
			message.setSubject("My first JavaMail program");

			//Setting the Message body
			body="Hello!";
			mainBody=new MimeBodyPart();
			mainBody.setDataHandler(new DataHandler(body, "text/plain"));
			mailBody.addBodyPart(mainBody);

			//Adding a single attachment
			fds=new FileDataSource(myFile);
			mimeAttach=new MimeBodyPart();
			mimeAttach.setDataHandler(new DataHandler(fds));
			mimeAttach.setFileName(fds.getName());
			mailBody.addBodyPart(mimeAttach);

			message.setContent(mailBody);

						Transport.send(message);
		}
		catch(java.io.UnsupportedEncodingException e)
		{
			System.out.println(e);
		}
		catch(MessagingException e)
		{
			System.out.println(e);
		}
		catch(IllegalStateException e)
		{
			System.out.println(e);
		}
	}

	}

public class TestMail01
{
	public static void main(String args[])
	{
		new MailSender();
	}
}

class MyAuthenticator extends Authenticator
{
	MyAuthenticator()
	{
		super();
	}

	protected PasswordAuthentication getPasswordAuthentication()
	{
		return new PasswordAuthentication("dider7", "MY_PASSWORD");
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 6 2009
Added on Apr 16 2005
35 comments
14,998 views