JavaMail Mail Development
Example 1, the first mail program.
The first step, install, configure the mail server Eyoumailserversetup.exe e-mail server
1.1 Click Settings, set to the following:
1.2 Click "New Account", register new mailbox ...
Through the above two steps, registered an account: fandong Password: 12345 email address: fandong@fd.com
Step two, configure the mail client. Outlook Express
2.1 Open the Outlook Express home page and click Tools Account
2.2 In the account panel click on the top right corner of the "add"-> Mail-> display Name: "Fandong"-> e-mail address: fandong@fd.com
And then:
--> account name: Fandong password:12345-> complete, the following page appears:
Double-click the account 127.0.0.1 and change the account 127.1.1.0 to: Fandong,
Configuration complete.
Third step, write the Java program to send mail:
Package cn.itcast.mail;
Import java.util.Properties;
Import javax.mail.Address;
Import Javax.mail.Message.RecipientType;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage; public class Sendsimplemail {public static void main (string[] args) throws Messagingexception {/******************** ///1. Setting up the message environment (the session class is used to define the environmental information required for the entire application) Properties Props=new properties ()
;
1.1 Set the protocol Props.setproperty to send mail ("Mail.transport.protocol", "SMTP");
1.2 Set the address of the Mail SMTP server Props.setproperty ("Mail.host", "127.0.0.1");
1.3 Set the way to connect to the mail server using login props.setproperty ("Mail.smtp.auth", "true");
1.4 Get Session Object session session=session.getinstance (props);
True indicates output debug information Session.setdebug (TRUE); /***********************************************************///2.Create a message in a mail environment MimeMessage Message=new mimemessage (session);
2.1 Set the subject of the message Message.setsubject ("Hahahahahahahha");
2.2 Set the sender of the message Message.setfrom (new internetaddress ("fandong@fd.com")); 2.3 Set Recipient/* Bcc: Dark Send (also to A and B, a and b do not know each other also received mail) * cc: CC (also sent to Abc,a for CC, b for dark Send, C for CC, C know a also have a, but I do not know Road B has a copy) * To: Ordinary send (mutual know)/Message.addrecipient (recipienttype.to, New internetaddress ("fandong@
Fd.com ")); Mass: message.addrecipients (recipienttype.to, New address[]{, New InternetAddress ("fandong1@fd.com"), new
InternetAddress ("fandong2@fd.com")});
2.4 Set the content of the message Message.settext ("Fandongfandongfandongyoujianneirong"); /***********************************************************///3. Send mail//transport class is the core API class that sends messages, and its instance object represents the implementation of the
A mail sending protocol to a mail object, such as the SMTP protocol//3.1 get Transport Transport Transport =session.gettransport (); 3.2 Set Connection/* fandong@fd.com------>fandong@fd.com * * Transport.connect ("Fandong", "12345");
3.3 Send mail Transport.sendmessage (message,new address[]{(New internetaddress ("fandong@fd.com"))};
3.4 Close Resource transport.close (); }
}
Fourth Step,
Click on Outlook Express's Drop-down menu to select Fandong, open its inbox, you can see the contents of the message sent by the Java program ...
Example 2, send a hypertext file mail, in the Outlook Express theme has garbled, you can set it to gb2312, there is no garbled.
Package cn.itcast.mail;
Import java.util.Properties;
Import javax.mail.Address;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import Javax.mail.Message.RecipientType;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart; public class Sendalternativemail {public static void main (string[] args) throws exception{/************************ ///1. Setting up the message environment (the session class is used to define the environmental information required for the entire application) Properties props=new Properti
ES ();
1.1 Set the protocol Props.setproperty to send mail ("Mail.transport.protocol", "SMTP");
1.2 Set the address of the Mail SMTP server Props.setproperty ("Mail.host", "127.0.0.1");
1.3 Set the way to connect to the mail server using login props.setproperty ("Mail.smtp.auth", "true"); 1.4 Getting Session Object session Session=session. getinstance (props);
True indicates output debug information Session.setdebug (TRUE); /***********************************************************///2. Create messages using the messaging environment MimeMessage Message=ne
W MimeMessage (session);
Setmessage (message); /***********************************************************///3. Send mail//transport class is the core of sending messages API class, whose instance object represents a mail delivery object that implements a mail delivery protocol, such as the SMTP protocol//3.1 get transport transport transport =session.get
Transport ();
3.2 Set Connection * * fandong@fd.com------>fandong@fd.com * *
Transport.connect ("fandong12388", "12345");
3.3 Send mail Transport.sendmessage (message,new address[]{(New internetaddress ("fandong12388@fd.com"))};
3.4 Close Resource transport.close (); public static void Setmessage (MimeMessage message) throws Messagingexception {Message.setsubject ("Mail containing hypertext");
Set Sender Message.setfrom (new internetaddress ("fandong12388@fd.com"));
Set recipient message.addrecipients (Recipienttype.to, Internetaddress.parse ("fandong12388@fd.com,fandong@fd.com"));
The body MimeBodyPart class represents a MIME message MimeBodyPart text=new MimeBodyPart ();
Text.setcontent ("
Example 3, send a message with a picture embedded in it.
Package cn.itcast.mail; public class Sendrelatedmail {public static void main (string[] args) throws exception{/**************************** ///1. Setting up the message environment (the session class is used to define the environmental information required for the entire application) Properties Props=new properties ()
;
1.1 Set the protocol Props.setproperty to send mail ("Mail.transport.protocol", "SMTP");
1.2 Set the address of the Mail SMTP server Props.setproperty ("Mail.host", "127.0.0.1");
1.3 Set the way to connect to the mail server using login props.setproperty ("Mail.smtp.auth", "true");
1.4 Get Session Object session session=session.getinstance (props);
True indicates output debug information Session.setdebug (TRUE); /***********************************************************///2. Create messages using the messaging environment MimeMessage Message=n
EW MimeMessage (session);
Setmessage (message); /***********************************************************/
//3. Send a message the//transport class is the core API class that sends messages, and its instance object represents a mail delivery object that implements a mail delivery protocol, such as the SMTP protocol//3.1 acquiring transport
Transport transport =session.gettransport ();
3.2 Set Connection * * fandong@fd.com------>fandong@fd.com * *
Transport.connect ("fandong12388", "12345");
3.3 Send mail Transport.sendmessage (message,new address[]{(New internetaddress ("fandong12388@fd.com"))};
3.4 Close Resource transport.close (); The public static void Setmessage (MimeMessage message) throws Messagingexception {Message.setsubject ("messages with embedded resources
");
Set Sender Message.setfrom (new internetaddress ("fandong12388@fd.com"));
Set recipient message.addrecipients (Recipienttype.to, Internetaddress.parse ("fandong12388@fd.com,fandong@fd.com"));
MimeBodyPart text=new MimeBodyPart ();
The cid:contentid abbreviation 1_jpg is a picture mark set below Text.setcontent ("
Example 4, send a message that contains attachments and hypertext. Mixed
Package cn.itcast.mail; public class Sendmixedmail {public static void main (string[] args) throws exception{/******************************
///1. Setting the context of a message (the session class is used to define the environmental information required for the entire application) Properties Props=new properties ();
1.1 Set the protocol Props.setproperty to send mail ("Mail.transport.protocol", "SMTP");
1.2 Set the address of the Mail SMTP server Props.setproperty ("Mail.host", "127.0.0.1");
1.3 Set the way to connect to the mail server using login props.setproperty ("Mail.smtp.auth", "true");
1.4 Get Session Object session session=session.getinstance (props);
True indicates output debug information Session.setdebug (TRUE); /***********************************************************///2. Create messages using the messaging environment MimeMessage Message=n
EW MimeMessage (session);
Setmessage (message); /***********************************************************///3. Send a message
The transport class is the core API class that sends messages, and its instance object represents a mail delivery object that implements a mail delivery protocol, such as an SMTP protocol//3.1 acquisition transport
Transport transport =session.gettransport ();
3.2 Set Connection * * fandong@fd.com------>fandong@fd.com * *
Transport.connect ("fandong12388", "12345");
3.3 Send mail Transport.sendmessage (message,new address[]{(New internetaddress ("fandong12388@fd.com"))};
3.4 Close Resource transport.close (); public static void Setmessage (MimeMessage message) throws Messagingexception {Message.setsubject ("Messages with Attachments")
;
Set Sender Message.setfrom (new internetaddress ("fandong12388@fd.com"));
Set recipient message.addrecipients (Recipienttype.to, Internetaddress.parse ("fandong12388@fd.com,fandong@fd.com"));
Define text MimeBodyPart text=new MimeBodyPart (); Text.setcontent ("
Right click on the contents of the attachment, you can save as ....
Example 5, send a message containing multiple attachments. (Add multiple attachments)
Package cn.itcast.mail;
public class SendMixedMail02 {public static void main (string[] args) throws exception{...}
public static void Setmessage (MimeMessage messages) throws Messagingexception {Message.setsubject ("Mail with Attachments");
Set Sender Message.setfrom (new internetaddress ("fandong12388@fd.com"));
Set recipient message.addrecipients (Recipienttype.to, Internetaddress.parse ("fandong12388@fd.com,fandong@fd.com"));
Define text MimeBodyPart text=new MimeBodyPart ();
Text.setcontent ("
Note: Before you develop a message, you should test that you can save the message locally before you send it, if it succeeds, and then send it (the code comment that will be sent when the test is sent, and the comments that will be fileoutputstream when you send it).
First step, test
Package cn.itcast.mail; public class SendMixedMail02 {public static void main (string[] args) throws exception{.../********************* ///3. Send mail (comment out when testing) the//transport class is the core API class that sends the message, and its instance object represents
A mail delivery object that implements a mail delivery protocol, such as SMTP protocol//3.1 acquires transport Transport Transport =session.gettransport ();
3.2 Set Connection * * fandong@fd.com------>fandong@fd.com * *
Transport.connect ("fandong12388", "12345");
3.3 Send mail//transport.sendmessage (message,new address[]{(New internetaddress ("fandong12388@fd.com"))};
3.4 Close Resource//transport.close (); public static void Setmessage (MimeMessage message) throws Messagingexception, IOException {message.setsubje
CT ("Mail containing attachments"); Sets the sender Message.setfrom (new internetaddress ("FANDONG12388@FD").com "));
Set recipient message.addrecipients (Recipienttype.to, Internetaddress.parse ("fandong12388@fd.com,fandong@fd.com"));
Define text MimeBodyPart text=new MimeBodyPart ();
Text.setcontent ("
The second step, under D disk can see more than one file 1.eml, double-click Open, if successfully opened, indicating that the message is edited correctly, you can send.
Step three, send.
Package cn.itcast.mail;
public class SendMixedMail02 {public
static void Main (string[] args) throws exception{
/***************** /
...
/***********************************************************/
//3. Send mail
transport transport = Session.gettransport ();
Transport.connect ("fandong12388", "12345");
Transport.sendmessage (message,new address[]{(New internetaddress ("fandong12388@fd.com"))};
Transport.close ();
}
public static void Setmessage (MimeMessage message) throws Messagingexception, IOException {
...
Save the message to the local (commented out)
//fileoutputstream fos=new fileoutputstream ("d:\\1.eml");
The message is written to the output stream
//message.writeto (FOS);
Fos.close ();
}
Example 6, a comprehensive example: send a message containing hypertext, pictures, and two attachments (the actual delivery can be tested before the test method, see Example 5).
Package cn.itcast.mail;