java mail api

  • Thread starter akhil srivastava
  • Start date
A

akhil srivastava

i m trying sending mail with thia code ingmail code running success
fully but with rediffmail or yahoo it cannot run plzz help me

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class mail1
{
String d_email = "(e-mail address removed)",
d_password = "im*******",
d_host = "smtp.gmail.com",
d_port = "587",
m_to = "(e-mail address removed)",
m_subject = "Testing",
m_text = "Hey, this is the testing email using
smtp.gmail.com.";
public static void main(String[] args)
{
String[] to={"(e-mail address removed)"};
// String[] cc={"(e-mail address removed)"};
// String[] bcc={"(e-mail address removed)"};
//This is for google
mail1.sendMail("(e-mail address removed)",
"KB*!O^L7",
"smtp.gmail.com,
"587",
"true",
"true",
true,

"javax.net.ssl.SSLSocketFactory",
"false",
to,
"hiiiiiiiiiii",
"hiii this is akhil sening mail via
smtp and java mail api");
}

public synchronized static boolean sendMail(
String userName,
String passWord,
String host,
String port,
String starttls,
String auth,
boolean debug,
String socketFactoryClass,
String fallback,
String[] to,
String subject,
String text){
Properties props = new Properties();
//Properties props=System.getProperties();
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);
System.out.println("properties");
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("(e-mail address removed)"));
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();
return true;
}
catch (Exception mex)
{
//mex.printStackTrace();
System.out.println("error"+mex);
return false;
}
}

}
 
T

Thufir

i m trying sending mail with thia code ingmail code running success
fully but with rediffmail or yahoo it cannot run plzz help me

Does it build? When it runs, what's the error?
 
R

Roedy Green

I have written a javamail app that does work. You can have a look to
see the basic skeleton.

https://wush.net/websvn/mindprod/listing.php?repname=mindprod&path=/com/mindprod/bulk/


i m trying sending mail with thia code ingmail code running success
fully but with rediffmail or yahoo it cannot run plzz help me

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class mail1
{
String d_email = "(e-mail address removed)",
d_password = "im*******",
d_host = "smtp.gmail.com",
d_port = "587",
m_to = "(e-mail address removed)",
m_subject = "Testing",
m_text = "Hey, this is the testing email using
smtp.gmail.com.";
public static void main(String[] args)
{
String[] to={"(e-mail address removed)"};
// String[] cc={"(e-mail address removed)"};
// String[] bcc={"(e-mail address removed)"};
//This is for google
mail1.sendMail("(e-mail address removed)",
"KB*!O^L7",
"smtp.gmail.com,
"587",
"true",
"true",
true,

"javax.net.ssl.SSLSocketFactory",
"false",
to,
"hiiiiiiiiiii",
"hiii this is akhil sening mail via
smtp and java mail api");
}

public synchronized static boolean sendMail(
String userName,
String passWord,
String host,
String port,
String starttls,
String auth,
boolean debug,
String socketFactoryClass,
String fallback,
String[] to,
String subject,
String text){
Properties props = new Properties();
//Properties props=System.getProperties();
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);
System.out.println("properties");
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("(e-mail address removed)"));
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();
return true;
}
catch (Exception mex)
{
//mex.printStackTrace();
System.out.println("error"+mex);
return false;
}
}

}

--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top