Hi friends. I used this program to send mails via JavaMail API. Thought it could be useful.
I have put system.out.println just for verification. U can remove those if u want.
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
public class SendMail {
public static boolean send(String[] to) {
System.out.println("send mail: " + to);
try {
String host = "smtp.mail.yahoo.co.in";
String from = "user name";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "asdfgh");
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] to_address = new InternetAddress[to.length];
int i = 0;
// To get the array of addresses
while (to[i] != null) {
to_address[i] = new InternetAddress(to[i]);
i++;
}
System.out.println(Message.RecipientType.TO);
i = 0;
while (to_address[i] != null) {
message.addRecipient(Message.RecipientType.TO, to_address[i]);
i++;
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
} catch (Exception e) {
System.out.println("this is the error: " + e);
return false;
}
}
public static void main(String args[]) {
String[] address = { "sample1@sample.com", "sample2@sample.com" };
new SendMail().send(address);
}
}
April 19, 2007 at 2:37 am
Hi All.
I also have a writeup that presents my EmailDelivery class (and example of how to use it) which I wrote to help me easily send emails from my Java applications using JavaMail. It supports relaying through SMTP servers that require authentication and has convenience methods for easily adding file attachments.
Check it out at:
http://timarcher.com/?q=node/53
July 30, 2008 at 11:36 am
Hii Tim,
I am working with this code and i got an ArrayIndexOutofBoundsException in the program.
February 22, 2009 at 1:34 pm
Hi Anand,
I want to send mail using the java mail api from a yahoo account. I used this code successfully for gmail but could not get it to work with Yahoo accounts. Can you let me know why is it so? Are the ports and host for yahoo correct?
February 23, 2009 at 8:40 am
@ Spag : I used this same program to send a mail from yahoo. It was working with this host and port numbers in 2006. I am not sure if they have changed it. I will check this and let you know if I find any info on this.
April 22, 2009 at 12:21 pm
After reading through this article, I just feel that I need more info. Could you share some resources ?
April 22, 2009 at 12:41 pm
@ Jane : Can you be more specific as to what resources you need ?
May 18, 2009 at 10:14 pm
hi,
i’m trying to write a script to send an email from my java application.
i used this code but i have a error wich tell me that “new SendMail()” cannot be resolved