Mail Send simple example-bean file

Source: Internet
Author: User


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 ();
}
}
}










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.