Must issue a STARTTLS command first

[nobr]Hello members

I am trying very much but coudlnt get out of it I m running an example : http://www.vipan.com/htdocs/javamail.html with some property modificationand n introduces smtpauthicator class.

using Gmail SMTP, I just want to check the running code later I will manage my own SMPT server.

from most of the posts on this prpblem I improve my code but couldnt successful

I have set

props.put("mail.smtp.starttls.enable","true");

also

java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

has checked with both

Session session = Session.getInstance(props , auth);

Session session = Session.getDefaultInstance(props);

my complet code is here:

import...

publicclass TestEmailextends HttpServlet{

protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException{

resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

out.println("<h1>Webmail</h1>");

// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!

String to ="to.email@gmail.com";

String from ="from.email@gmail.com";

// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!

String host ="gmail-smtp.l.google.com";//"gmail-smtp.l.google.com";

java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

// Create properties, get Session

Properties props =new Properties();

// If using static Transport.send(),

// need to specify which host to send it to

props.put("mail.transport.protocol","smtp");

props.put("mail.smtp.starttls.enable","true");

props.put("mail.smtp.port","587");

props.put("mail.smtp.host", host);

props.put("mail.smtp.auth","true");

// To see what is going on behind the scene

props.put("mail.debug","true");

try{

Authenticator auth =new SMTPAuthenticator();

Session session = Session.getInstance(props , auth);

// Instantiatee a message

Message msg =new MimeMessage(session);

// Set message attributes

msg.setFrom(new InternetAddress(from));

InternetAddress[] address ={new InternetAddress(to)};

msg.setRecipients(Message.RecipientType.TO, address);

msg.setSubject("Test E-Mail through Java");

msg.setSentDate(new Date());

// Set message content

msg.setText("This is a test of sending a "

+"plain text e-mail through Java.\n" +"Here is line 2.");

// Send the message

out.println("preparing to send<br>");

Transport.send(msg);

}catch (MessagingException mex){

// Prints all nested (chained) exceptions as well

//mex.printStackTrace();

out.println(mex.toString());

}

}

}

class SMTPAuthenticatorextends javax.mail.Authenticator{

public PasswordAuthentication getPasswordAuthentication(){

String username ="from.email@gmail.com";

String password ="password";

returnnew PasswordAuthentication(username, password);

}

}

I m sure I got solution from u ppl.

Thx in advance

- Tahir[/nobr]

[5305 byte] By [tahirakrama] at [2007-10-3 2:33:55]
# 1

Hello

Here is my code that will connect and send an email via gmail.

Hope it helps!

The problem most likely is that you're doing the security (SSL) the wrong way.

Good luck! ^^

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

public class Main

{

String d_email = "ADDRESS@gmail.com",

d_password = "PASSWORD",

d_host = "smtp.gmail.com",

d_port = "465",

m_to = "EMAIL ADDRESS",

m_subject = "Testing",

m_text = "Hey, this is the testing email.";

public Main()

{

Properties props = new Properties();

props.put("mail.smtp.user", d_email);

props.put("mail.smtp.host", d_host);

props.put("mail.smtp.port", d_port);

props.put("mail.smtp.starttls.enable","true");

props.put("mail.smtp.auth", "true");

//props.put("mail.smtp.debug", "true");

props.put("mail.smtp.socketFactory.port", d_port);

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try

{

Authenticator auth = new SMTPAuthenticator();

Session session = Session.getInstance(props, auth);

//session.setDebug(true);

MimeMessage msg = new MimeMessage(session);

msg.setText(m_text);

msg.setSubject(m_subject);

msg.setFrom(new InternetAddress(d_email));

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

Transport.send(msg);

}

catch (Exception mex)

{

mex.printStackTrace();

}

}

public static void main(String[] args)

{

Main blah = new Main();

}

private class SMTPAuthenticator extends javax.mail.Authenticator

{

public PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication(d_email, d_password);

}

}

}

NightCabbagea at 2007-7-14 19:32:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Thank you NightCabbageIts working, You have made my life very easy thank u very very muchrgdTahir Pakistan
tahirakrama at 2007-7-14 19:32:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

A Blind man in need of help from the scratch.

I have this same problem trying to send an email using open fitness program, which has an in built email sending client. However when I include my smtphm.myserver.ca i get an error mesage that is in the subject.

I have read everywhere and it seems like executing the code is simillar. Here is the problem. Me = a newbie (nubbie) I mean I really dont know my elbow from my rear end, but I can follow instructions. As I read every solution starts somewhere, but honestly I dont even know where to start to be able to send email.

may I ask a kind human being to hold me by my wrist and help me step by step as in

1: click _ _ _

2:Click._ _ _

I am soo sorry I feel Dumb already please help.

kamalibraa at 2007-7-14 19:32:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

I think you need help beyond what I can offer here.

I don't know what "open fitness program" is, but you probably

should start with the owner of that program to ask for help.

If the email sending capability is embedded in that program

it may or may not be using JavaMail and it may or may not

be able to be configured without changing the program. You

need to talk to the owner of the program to find out.

bshannona at 2007-7-14 19:32:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
This is the same problem for all. blind or not one solution will solve all of this. pls guide the start folder for me to include the code..
kamalibraa at 2007-7-14 19:32:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

Thanx for the code to send mail from gmail..

i have tried the same for the yahoo by changing the smtp address and port number and also found 1 site for smtp port numbers (refer this site http://www.snappermail.com/support/isp.cfm#19 ) .. but i am not able to use it..

plz tell how to send using yahoo account...

my id is puneetaggarwal16@yahoo.com

Message was edited by:

puneetaggarwal

puneetaggarwala at 2007-7-14 19:32:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
Explain "not able to use it".Have you purchased a Yahoo Mail Plus account?
bshannona at 2007-7-14 19:32:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...