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);
}
}
}
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...
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();
}
}>