Use spring to send e-mails with attachments (intra-station and off-site transfer)

Source: Internet
Author: User
Tags email account mail account microsoft outlook mail microsoft outlook

Introduction of JavaMail

JavaMail, as the name implies, provides developers with programming interfaces for dealing with e-mail. It is the API that Sun releases to handle email. It is easy to perform some common mail transfer.

Although JavaMail is one of the Sun's APIs, it is not currently added to the standard Java SDK (Java Development Kit), which means that you must download the JavaMail file separately before you use it. In addition, you also need to have Sun's JavaBeans Activation Framework (JAF). The operation of the JavaBeans Activation framework is complex, which is simply to say that JavaMail's operation must depend on its support. Using the path required to specify these files under Windows 2000 is similar to other operating systems.

JavaMail is an optional package, so you need to download it first from java.sun.com if you need to use it. The latest version is JavaMail1.4, which requires JavaBean Activation framework support when using JavaMail, so you also need to download JAF. Install JavaMail just need to add them to the classpath, if you do not want to modify classpath, you can directly copy their jar package directly to Java_home/lib/ext.   So the javamail is ready to be installed. The core classes used to process e-mail in the JavaMail package are: Session,message,address,authenticator,transport,store,folder and so on. Session defines a basic mail session that needs to read from the properties like mail server, user name and password, etc.

1. Mail protocol

Mainly include:

SMTP protocol: Simple Mail Transfer Protocol, which is simply a message transfer protocol for sending e-mail

POP3 protocol: Post Office Protocol 3, a third version of the Post Office Protocol, for receiving mail

IMAP protocol: Internet Message Access Protocol, an Internet messaging protocol, is an alternative protocol to POP3

2. Build the James Mail server

James is an open source project for Apache, pure Java implementation

Build the James Server

1) Download Apache-james-2.3.2.zip decompression

2) Run the Run.bat in the Bin directory to start the server

3) Configuring the server via Apps\james\sar-inf\config.xml

Must note: First to the bin run together with the non-Chinese directory to the Control Panel to turn on the Telnet client

Telnet localhost 4555

3. Install outlook[mail client]

Product Key: PQDV9-GPDV4-CRM4D-PHDTH-4M2MT

Create user account

One, using Telnet to connect James's remote administration Tool

Second, log in as Administrator

Iii. adding users using the AddUser command

4. Configuring the Outlook mail client

For easy viewing, you can configure the Microsoft Outlook mail client to ensure that the James Mail server is up and running in Microsoft Outlook.

Select Tools, Options, to open the Options panel. Select "Mail Settings" and click "Email account" to open the "Account Settings" panel. Create a new mail account under the E-mail tab

5. Case [Build James Mail server]

Requirements Description:

Build the James Mail server on this machine, and customize the name of the server.

Create two test users.

Configure one of the test users in Microsoft Outlook to be an Outlook mail account

6. Send e-mail using javamail (case)

Demand:

Using JavaMail technology, the implementation from a account to the B account sent an email, titled "Meeting Notice", the message is "XX Hello!" Please arrive at the B01 meeting room tomorrow 16:00 for a technical seminar. "See whether messages sent by mail programs are sent successfully through the Outlook client

Key code:

Create a class Emailauthenticator and inherit from authenticator, and populate the user name and password

View Code

Create mail class to set message information:

View Code

Test class:

View Code

Send mail with an attachment

Mailwithattachment:

View Code

Test class:

View Code

Applicationcontext.xml: Large Configuration

View Code

Send mail with pictures

mail.com

View Code

Mailservice:

View Code

Sendmailaction:

View Code

Applicationcontext.xml: Large Configuration

View Code

Struts.xml

View Code

Xml

View Code

index.jsp

View Code

sendmail_success.jsp

View Code

Use spring to send e-mails with attachments (intra-station and off-site transfer)

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.