This example for you to share the mail transceiver function of the specific implementation code for your reference, the specific contents are as follows
Preparation work, environment setting:
1. Build a mail server locally
Easy mail server, Eyoumailserversetup.exe
2. New Mailbox Account
John send email to Dick.
Step 1:
new domain name: tools, server settings, single domain name box input itcast.com
Step 2:
New Mailbox Account: Zhangsan@itcast.com
Lisi@itcast.com
3. Install Foxmail
Configure mail forwarding Server (SMTP): localhost 25
Mail receiving server (POP3): localhost 110
Then create a new account, you can receive mail!
Attention
If this is a Web project, there may be a problem because Java EE has a mail feature from it.
We want to use our own Mail.jar file features! Need to remove the Java EE Mail package!
Use:
JavaMail Development, first introduce JAR file:
Activation.jar "If you are using jdk1.6 or above, you can use this jar file."
Mail.jar "Mail Send Core package"
/** * 1.
Send a normal message * @author Jie.yuan * * */public class App_sendmail {@Test public void Testsend () throws Exception { 0.
Message Parameter Properties Prop = new properties (); Prop.put ("Mail.transport.protocol", "SMTP"); Specify protocol prop.put ("Mail.smtp.host", "localhost"); Host stmp.qq.com prop.put ("Mail.smtp.port", 25); Port Prop.put ("Mail.smtp.auth", "true"); User Password Authentication prop.put ("Mail.debug", "true"); Debug mode//1.
Create a conversation session sessions for a message = Session.getdefaultinstance (prop); 2.
Create a message body object (entire mail object) mimemessage message = new MimeMessage (session); 3.
Set the message body parameters://3.1 title Message.setsubject ("My first email");
3.2 Mail Delivery time message.setsentdate (new Date ());
3.3 Sender Message.setsender (new internetaddress ("zhangsan@itcast.com"));
3.4 Receiver Message.setrecipient (recipienttype.to, New internetaddress ("lisi@itcast.com")); 3.5 content Message.settext ("Hello, has been sent to success!") Text ... "); Simple Plain Text message message.saVechanges (); Save the message (optional)//4.
Send Transport trans = Session.gettransport ();
Trans.connect ("Zhangsan", "888");
Send mail trans.sendmessage (message, message.getallrecipients ());
Trans.close ();
}
}
with picture
/** * Message with picture resources * @author Jie.yuan */public class App_2sendwithimg {//initialization parameters private static Properties prop;
Sender private static internetaddress Sendman = null;
static {prop = new Properties (); Prop.put ("Mail.transport.protocol", "SMTP"); Specify protocol prop.put ("Mail.smtp.host", "localhost"); Host stmp.qq.com prop.put ("Mail.smtp.port", 25); Port Prop.put ("Mail.smtp.auth", "true"); User Password Authentication prop.put ("Mail.debug", "true");
Debug mode try {Sendman = new internetaddress ("zhangsan@itcast.com");
catch (Addressexception e) {throw new RuntimeException (e); } @Test public void Testsend () throws Exception {//1. Create a message session sessions = Session.getdefaultinstan
CE (prop); 2.
Create a Mail object mimemessage message = new MimeMessage (session); 3.
Set parameters: Title, sender, recipient, send time, Content Message.setsubject ("with picture Mail");
Message.setsender (Sendman); Message.setrecipient (recipienttype.to, new inteRnetaddress ("lisi@itcast.com"));
Message.setsentdate (New Date ()); /*************** Set message content: Multifunction user mail (related) *******************///4.1 Building a multi-purpose message block mimemultipart related = new Mimemu
Ltipart ("related");
4.2 Building Multi-function Message Block content = Left text + right picture resource mimebodypart content = new MimeBodyPart ();
MimeBodyPart resource = new MimeBodyPart ();
Set specific content: A. resources (picture) String FilePath = App_2SendWithImg.class.getResource ("8.jpg"). GetPath ();
DataSource ds = new Filedatasource (new File (FilePath));
DataHandler handler = new DataHandler (DS);
Resource.setdatahandler (handler); Resource.setcontentid ("8.jpg"); Set the resource name, give the foreign key reference//set the specific content: B. Text Content.setcontent (" good haha!")
"," Text/html;charset=utf-8 ");
Related.addbodypart (content);
Related.addbodypart (Resource);
/*******4.3 constructs complex messages quickly, adding them to the message ********/message.setcontent (related); 5.
Send Transport trans = Session.gettransport (); Trans.connect ("Zhangsan", "888");
Trans.sendmessage (Message, message.getallrecipients ());
Trans.close ();
}
}
picture + attachment
/** * 3.
Messages with picture resources and attachments * @author Jie.yuan * */public class App_3imgandatta {//initialization parameters private static Properties prop;
Sender private static internetaddress Sendman = null;
static {prop = new Properties (); Prop.put ("Mail.transport.protocol", "SMTP"); Specify protocol prop.put ("Mail.smtp.host", "localhost"); Host stmp.qq.com prop.put ("Mail.smtp.port", 25); Port Prop.put ("Mail.smtp.auth", "true"); User Password Authentication prop.put ("Mail.debug", "true");
Debug mode try {Sendman = new internetaddress ("zhangsan@itcast.com");
catch (Addressexception e) {throw new RuntimeException (e); } @Test public void Testsend () throws Exception {//1. Create a message session sessions = Session.getdefaultinstan
CE (prop); 2.
Create a Mail object mimemessage message = new MimeMessage (session); 3.
Set parameters: Title, sender, recipient, send time, Content Message.setsubject ("with picture Mail");
Message.setsender (Sendman); Message.setrecipient (recipienttype.to, NEW internetaddress ("lisi@itcast.com"));
Message.setsentdate (New Date ());
* * with attachment (picture) mail Development///build a total message block mimemultipart mixed = new Mimemultipart ("mixed");
---> Total mail quickly, set to Message.setcontent in the Mail object (mixed);
Left: (text + picture resources) MimeBodyPart (= new MimeBodyPart);
Right: Attachment MimeBodyPart right-hand = new MimeBodyPart ();
Set to total message Block Mixed.addbodypart (left);
Mixed.addbodypart (right);
/****** attachment ********/String Attr_path = This.getclass (). GetResource ("A.docx"). GetPath ();
DataSource Attr_ds = new Filedatasource (new File (Attr_path));
DataHandler Attr_handler = new DataHandler (ATTR_DS);
Right.setdatahandler (Attr_handler);
Right.setfilename ("A.docx"); /*************** Set message content: Multifunction user mail (related) *******************///4.1 Building a multi-purpose message block mimemultipart related = new Mimemu
Ltipart ("related");
----> Set to the total mail fast left left.setcontent (related); 4.2 Building Multi-function Message Block content = Left text + right picture resource MimeBodyPart Content = new MimeBodyPart ();
MimeBodyPart resource = new MimeBodyPart ();
Set specific content: A. resources (picture) String FilePath = App_3ImgAndAtta.class.getResource ("8.jpg"). GetPath ();
DataSource ds = new Filedatasource (new File (FilePath));
DataHandler handler = new DataHandler (DS);
Resource.setdatahandler (handler); Resource.setcontentid ("8.jpg"); Set the resource name, give the foreign key reference//set the specific content: B. Text Content.setcontent (" good haha!")
"," Text/html;charset=utf-8 ");
Related.addbodypart (content);
Related.addbodypart (Resource); 5.
Send Transport trans = Session.gettransport ();
Trans.connect ("Zhangsan", "888");
Trans.sendmessage (Message, message.getallrecipients ());
Trans.close ();
}
}
The above is the entire content of this article, I hope to learn Java program to help you.