The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Program to send mail

Posted by Arun Sacheti on October 13, 2000 at 5:15 PM

Here is a sample code

you need the javax.mail package for this.

I am using the yahoo smtp server. You can replace it with any other smtp server.

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

public class MailSend{
public MailSend(){
try{
String smtphost ="smtp.mail.yahoo.com" ;
String from = "sacheti@yahoo.com";
String to = "sarcot@yahoo.com" ;
// start a session
Properties prop = System.getProperties();
prop.put("mail.smtp.host",smtphost);
Session session1 = Session.getInstance(prop,null);
// construct a message
MimeMessage msg = new MimeMessage(session1);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
msg.setSubject("Example");
msg.setText("from mail program");
// connect to the transport
Transport trans = session1.getTransport("smtp");
trans.connect(smtphost,"userid","password");
// send the message
trans.sendMessage(msg,msg.getAllRecipients());
//smtphost
trans.close();
}catch(Exception e){
e.printStackTrace();
}
}// Constructor

public static void main(String[] s){
MailSend ms = new MailSend();
}
}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us