Simplesendmessage.java
Import java.util.*;
Import javax.mail.*;
Import javax.mail.internet.*;
Import javax.activation.*;
public class Simplesendmessage {
public static void Main (string[] args) {
Collect the necessary information to send a simple message
Make sure to replace the values for host
Valid information.
Host-must be a valid SMTP server so you currently have
Access to.
To-whoever is going to get your email
From-whoever you want. Just remember that many SMTP
Servers would validate the domain of the From
Before allowing the mail to be sent.
String host = "server.myhost.com";
String to = "YourFriend@somewhere.com";
String from = "MeMeMe@myhost.com";
String subject = "JSP rules!";
String MessageText = "I am sending a message using the"
+ "JavaMail Api.\ni can include any text I want.";
Boolean sessiondebug = false;
Create some properties and get the default session.
Properties props = System.getproperties ();
Props.put ("Mail.host", host);
Props.put ("Mail.transport.protocol", "SMTP");
Session session = Session.getdefaultinstance (props, null);
Set Debug on "So we can" what is going on
Passing false would not echo debug info, and passing true
Will.
Session.setdebug (Sessiondebug);
try {
Instantiate a new mimemessage and fill it with the
Required information.
msg = new MimeMessage (session);
Msg.setfrom (New InternetAddress (from));
Internetaddress[] Address = {new internetaddress (to)};
Msg.setrecipients (Message.RecipientType.TO, address);
Msg.setsubject (subject);
Msg.setsentdate (New Date ());
Msg.settext (MessageText);
Hand the message to the default transport service
For delivery.
Transport.send (msg);
}
catch (Messagingexception Mex) {
Mex.printstacktrace ();
}
}
}