Source: Internet
Author: User

Tags: requires IMA trace throws encoded password Bat Apach REM

I. Related Concepts of e-mail

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

second, Build James Mail server

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

Build the James Server

① Download Unzip

② run the Run.bat in the Bin directory to start the server [Telnet localhost 4555]

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

Note: go to the bin and run Together. In Non-chinese directories, The Telnet client can be turned on by the control panel.

third, Install Outlook[mail client]


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

1: Enter Telnet command, Specify the server address and port number of the connection

Iv. 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

V. 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

Vi. sending e-mail using javamail (case)


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

Create the Mail class (set message information):

 packagecn.happy.tets;Importjava.util.Date;Importjava.util.Properties;Importjavax.mail.Authenticator;Importjavax.mail.Message;Importjavax.mail.Session;Importjavax.mail.Transport;Importjavax.mail.internet.InternetAddress;Importjavax.mail.internet.MimeMessage; public classMail {PrivateString mailserver,from,to,mailsubject,mailcontent; PrivateString username,password;  publicMail () {//set up message information//User name for authentication loginUsername= "[email protected]"; //Authentication Passwordpassword= "cmy"; //mail server for the Certified mailboxMailserver= ""; //Sender InformationFrom= "cmy"; //Recipient Informationto= "[email protected]"; //message Headermailsubject= "oh, hehe, haha"; //Message Contentmailcontent=, "i'll go."; }  //setting up a mail server@SuppressWarnings ("static-access") public  voidsend () {Properties prop=system.getproperties (); //Specify mail serverProp.put ("", mailserver); //whether to turn on authenticationProp.put ("mail.smtp.auth", "true"); //of the SMTP protocolProp.put ("mail.smtp.port", "25"); //Generate session ServiceEmailauthenticator mailauth=NewEmailauthenticator (username, password); Session mailsession=session.getinstance (prop, (Authenticator) mailauth); Try {           //encapsulates a Message objectMessage message=NewMimeMessage (mailsession); Message.setfrom (NewInternetAddress (from));//SenderMessage.setrecipient (Message.RecipientType.TO,NewInternetAddress (to));//RecipientMessage.setsubject (mailsubject); //setting content (setting Character Set handling garbled issues)Message.setcontent (mailcontent, "text/html;charset=gbk"); Message.setsentdate (NewDate ()); //Create a transport instance, send a messageTransport Tran=mailsession.gettransport ("smtp");           Tran.send (message,message.getallrecipients ());                   Tran.close (); } Catch(Exception E) {e.printstacktrace (); }  }}

Test Class:

 package cn.happy.tets;  public class Test {    publicstaticvoid  main (string[] args) {        mail mail=  New  Mail ();        Mail.send ();        System.out.println ("success!" );    }}

Vii. send mail with attachments (integrated with Spring)

 public classmailwithattachment {PrivateJavamailsender mailsender;//must use Javamailsender     public voidSetmailsender (javamailsender Mailsender) { this. MailSender =mailsender; }          public voidSend ()throwsmessagingexception,ioexception{mimemessage mimemessage=Mailsender.createmimemessage (); Mimemessagehelper Helper=NewMimemessagehelper (mimemessage,true, "UTF-8"); Helper.setfrom ("[email protected]"); Helper.setto ("[email protected]"); Helper.setsubject ("good for the next"); Helper.settext ("not good"); //Add attachment 1Classpathresource file1 =NewClasspathresource ("/cn/bdqn/attachfiles/test.doc");        Helper.addattachment (file1.getfilename (), file1.getfile ()); //Add attachment 2: The file name of the attachment is chinese, you need to encode and convert the file name to solve the garbled problemClasspathresource file2 =NewClasspathresource ("/cn/bdqn/attachfiles/attachment Test File. doc");        Helper.addattachment (mimeutility.encodeword (file2.getfilename ()), file2.getfile ());    Mailsender.send (mimemessage); }}

Test Class:

 public classMailTest { public Static voidmain (string[] Args) {applicationcontext context=NewClasspathxmlapplicationcontext ("applicationcontext.xml"); /*test messages with attachments*/        Try{mailwithattachment Mailwithattach= (mailwithattachment) Context.getbean ("mailwithattachment");        Mailwithattach.send (); }Catch(Exception e) {System.out.print (e.tostring ()); }    }}  

Application.xml file:

 public classMailTest { public Static voidmain (string[] Args) {applicationcontext context=NewClasspathxmlapplicationcontext ("applicationcontext.xml"); /*test messages with attachments*/        Try{mailwithattachment Mailwithattach= (mailwithattachment) Context.getbean ("mailwithattachment");        Mailwithattach.send (); }Catch(Exception e) {System.out.print (e.tostring ()); }    }}  


Related Article

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: