Remember, first you have to add jar file and headers as mentioned here.
http://mihirans.blogspot.com/2012/05/sending-emails-in-java-application.html
YAHOO
public void sendYahooMail(String sender, String passwd, String [] receiver, String sub, String body) throws MessagingException
{
String host = "smtp.mail.yahoo.com";
String from = sender;
String pass = passwd;
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
String[] to = receiver;
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(sub);
message.setText(body);
Transport transport = session.getTransport("smtps");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
HOTMAIL
public void sendHotMail(String sender, String passwd, String [] receiver, String sub, String body) throws MessagingException
{
String host = "smtp.live.com";
String from = sender;
String pass = passwd;
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = receiver; // added this line
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(sub);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
Here's my Interface
And here is the button click even of "Send Email" button.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
char [] passwd = jPasswordField1.getPassword();
String spass = "";
String Client = jComboBox1.getSelectedItem().toString();
for (int i = 0; i < passwd.length; i++)
{
spass += passwd[i];
}
String from = jTextField1.getText()+"@"+Client;
String pass = spass.toString();
String [] to = jTextField3.getText().toString().split(",");
String sub = jTextField4.getText().toString();
String body = jTextArea1.getText().toString();
SendMail s = new SendMail();
try
{
if(Client == "gmail.com"){
s.sendGMail(from,pass,to,sub,body);
}
else if(Client == "yahoo.com"){
s.sendYahooMail(from,pass,to,sub,body);
}
else{
s.sendHotMail(from,pass,to,sub,body);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
Ok guyz, that's it. See you again.
Thank You !