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]

