mail in jsp

Hi,i m using javamail in my project....using local host i cud easily do it but i wanna use gmail as my ...so can any one help me in doing it.....i want simple code without using java beans...just in jsp n servletthank u in advance who ever helps me
[298 byte] By [anshu_it_surata] at [2007-11-27 7:30:42]
# 1

Hope this may be useful

package com;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

public class SampleEmail

{

String d_email = "",

d_password = "",

d_host = "",

d_port = "",

m_to = "",

m_subject = "Testing",

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

public SampleEmail()

{

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)

{

SampleEmail blah = new SampleEmail();

}

private class SMTPAuthenticator extends javax.mail.Authenticator

{

public PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication(d_email, d_password);

}

}

}

SARAV_RSa at 2007-7-12 19:10:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Did you understand his question? He has it working at localhost, but he want to use the GMail SMTP server.Go to the GMail FAQ/documentation/service/whatever and lookup/ask if they have a SMTP server available.
BalusCa at 2007-7-12 19:10:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Check it out this link: http://java.sun.com/products/javamail/FAQ.html#gmail
SARAV_RSa at 2007-7-12 19:10:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
m sorry to say but its not working fine....it neither gives any error nor the expected result is received...thx
anshu_it_surata at 2007-7-12 19:10:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

thx the code worked with little changes.....

but now there is another problem that is of multiple recipients....

i used arraylist "list" to retrive data from database n used itt in message.AddRecipients(list.get(i).tostring).......

but its not working well.

while debugging it doesnot pass the control to line

transport.sendMessage(msg1, msg1.getAllRecipients());

can u plz help me then? its urgent...

anshu_it_surata at 2007-7-12 19:10:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
without using arraylist, to send multiple reciepents address.msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
SARAV_RSa at 2007-7-12 19:10:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

m sorry to bother u again

there is again an error related to authentication

it says authentication failed

here is the code

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

response.setContentType("text/html");

PrintWriter out=response.getWriter();

String[] to={"anshu_thedifferent@yahoo.com","prety_twlip@yahoo.com"};

String[] cc={"prety_twlip@yahoo.com","anshu_thedifferent@yahoo.com"};

String[] bcc={"anshu_thedifferent@yahoo.com"};

String userName="anshuthedifferent@gmail.com";

String passWord="targethigh";

String host="smtp.gmail.com";

String port="465";

String starttls="true";

String auth="true";

boolean debug=true;

String socketFactoryClass="javax.net.ssl.SSLSocketFactory";

String fallback="false";

//String[] to=to;

String subject="hi there";

String text="this is testing mail";

Properties props = new Properties();

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

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

if(!"".equals(port))

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

if(!"".equals(starttls))

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

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

if(debug){

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

}else{

props.put("mail.smtp.debug", "false");

}

if(!"".equals(port))

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

if(!"".equals(socketFactoryClass))

props.put("mail.smtp.socketFactory.class",socketFactoryClass);

if(!"".equals(fallback))

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

try

{

Session session = Session.getDefaultInstance(props, null);

session.setDebug(debug);

MimeMessage msg = new MimeMessage(session);

msg.setText(text);

msg.setSubject(subject);

msg.setFrom(new InternetAddress("anshuthedifferent@gmail.com"));

for(int i=0;i<to.length;i++){

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

}

for(int i=0;i<cc.length;i++){

msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));

}

for(int i=0;i<bcc.length;i++){

msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));

}

msg.saveChanges();

Transport transport = session.getTransport("smtp");

transport.connect(host, userName, passWord);

transport.sendMessage(msg, msg.getAllRecipients());

transport.close();

out.println("message sent");

//return true;

}

catch (MessagingException mex) {

mex.printStackTrace();

}

}>

anshu_it_surata at 2007-7-12 19:10:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...