Using the JavaMail API to write mail-sending programs with attachments

Source: Internet
Author: User

Using the JavaMail API provided by Sun, you can easily develop mail-sending programs. You may already be able to use it to send a simple text, but do you want to make your program send attachments like Outlook? After a brief introduction of JavaMail, this article explains in detail a complete messenger JavaBean and a very lightweight servlet.

(Readers without loading the JavaMail API can download to this site and set up classpath according to Readme.txt)

A, javamail some of the classes we need

1.Properties

JavaMail requires properties to create a Session object whose property value is the host that sends the message, such as:

Properties props = new Properties ();
props.put("mail.smtp.host", "smtp.xxxx.com");//可以换上你的smtp主机名,就像你在OutLook中设置smtp主机名一样。

2.Session

All JavaMail programs require at least one or all of the conversation goals.

Session session = Session.getInstance(props, null);

3.MimeMessage

The information object will truly reflect the message you send.

MimeMessage msg = new MimeMessage(session);

4.Transport

The sending of a message is done by transport:

Transport.send(msg);

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.