Send email via Java code 1 (58), java code send email 58
Email protocol:
Email: SMTP (Simple Mail Transport Protocal)
Email receiving protocol: pop3 (Post Office Protocal 3)
IMAP can also receive emails by sending emails with the new protocol.
(Step-by-step interaction with servers)
SMTP:
Ehlo <space> <your domain name> <press enter> Ehlo-Server |
Auth <SP> Login <CRLF> Login Request |
Mail <SP> From: <reverse-path> <CRLF> Sender email: www@aa.com |
Rcpt <SP> To: <forword-path> <CRLF> Recipient address: eee@aa.com |
Data <CRLF> The following are data |
Quit <CRLF> Exit |
Email sending and receiving process:
Generally, smtp and pop3 are two servers (host ).
The Smtp mail port is 25.
POP3 port 110.
Mail example
1 install foxmail:
// Base64 encode the user name and password
@ Test
Public VoidBase64 (){
String name = "wj_leaf12345 ";
String pwd = "1qaz2wsx ";
BASE64Encoder en =NewBASE64Encoder ();
Name = en. encode (name. getBytes ());
Pwd = en. encode (pwd. getBytes ());
System.Err. Println (name );
System.Err. Println (pwd );
}
3. Send emails using java code
To send an email in java, you must import a new package.
Mail. jar-core package for sending emails
Activation. jar-encryption of users and passwords.
There are three core classes in mail. jar:
Javax. mail. Session-refers to the Session with the mail server. You only need one project.
Javax. mail. Message (Interface)-prepare to send data information.
MimeMessage-you can set data types.
Transport-it has a method to send messages.
Step 1: import two jar packages
Step 2: send a simple email
Public VoidSendMail ()ThrowsException {
// Step 1: declare the properties Object Information
Properties prop =NewProperties ();
// Set which server to connect
Prop. setProperty ("mail. host", "smtp.126.com ");
// Set whether to verify
Prop. setProperty ("mail. smtp. auth", "true ");
// Step 2: declare the user name and password
Authenticator auth =NewAuthenticator (){
// This request returns the user and password object
PublicPasswordAuthentication getPasswordAuthentication (){
PasswordAuthentication pa =
NewPasswordAuthentication ("aaa", "sss ");
ReturnPa;
}
};
/// Step 2: Obtain the Session object
Session session =
Session.GetDefaultInstance(Prop, auth );
// Set the session debugging mode
Session. setDebug (True);
// Step 3: declare Information
MimeMessage mm1 =
NewMimeMessage (session );
// Step 4: Set the sender email
Address from =NewInternetAddress ("wj@126.com ");
Mm1.setFrom (from );
// Step 5: Set the recipient
Mm1.setRecipient (RecipientType.TO,NewInternetAddress ("wj@163.com "));
Mm1.setRecipient (RecipientType.CC,NewInternetAddress ("554@qq.com "));
Mm1.setRecipient (RecipientType.BCC,NewInternetAddress ("wj@ss.cn "));
// Step 6: Set the topic
Mm1.setSubject ("this is an email sent in Java 3 ");
Mm1.setContent ("Hello, this is an email sent in java, 3333 try again", "text/plain; charset = UTF-8 ");
// Step 7:
Transport.Send(Mm1 );
}
Step 3: send an email with a hyperlink
Mm1.setSubject ("this is an email sent using Java sfasdf3 ");
Mm1.setContent ("Hello, this is an email sent in java, <a href = 'HTTP: // www.baidu.com '> Baidu </a>", "text/html; charset = UTF-8 ");
// Step 7:
Transport.Send(Mm1 );
Step 4: email with a letter
Public VoidSendFile ()ThrowsException {
Properties p =NewProperties ();
P. setProperty ("mail. host", "smtp.163.com ");
P. setProperty ("mail. smtp. auth", "true ");
Session s = Session.GetDefaultInstance(P,NewAuthenticator (){
@ Override
PublicPasswordAuthentication getPasswordAuthentication (){
Return NewPasswordAuthentication ("ww", "123 ");
}
});
S. setDebug (True);
// Declare MimeMessage
MimeMessage msg =NewMimeMessage (s );
Msg. setFrom (NewInternetAddress ("ww@163.com "));
Msg. setRecipient (RecipientType.TO,NewInternetAddress ("ww@126.com "));
Msg. setSubject ("image ");
// Step 1: declare the multi-process Part
MimeMultipart mm =NewMimeMultipart ();
// Step 2: declare
MimeBodyPart body1 =NewMimeBodyPart ();
// Step 3: Set the delimiter
DataSource ds =NewFileDataSource (NewFile ("./img/a.jpg "));
DataHandler dh =NewDataHandler (ds );
Body1.setDataHandler (dh );
// The name must be set.
Body1.setFileName (MimeUtility.EncodeText("Beauty .jpg "));
MimeBodyPart body2 =NewMimeBodyPart ();
// Step 3: Set the delimiter
DataSource ds2 =NewFileDataSource (NewFile ("./img/B .jpg "));
DataHandler dh2 =NewDataHandler (ds2 );
Body2.setDataHandler (dh2 );
// The name must be set.
Body2.setFileName (MimeUtility.EncodeText("Beauty 2.jpg "));
MimeBodyPart body3 =NewMimeBodyPart ();
// Step 3: Set the delimiter
DataSource ds3 =NewFileDataSource (NewFile ("./img/mworkflow "));
DataHandler dh3 =NewDataHandler (ds3 );
Body3.setDataHandler (dh3 );
// The name must be set.
Body3.setFileName (MimeUtility.EncodeText("The last century "));
// Add body1 to mm
Mm. addBodyPart (body1 );
Mm. addBodyPart (body2 );
Mm. addBodyPart (body3 );
Msg. setContent (mm );
// Send
Transport.Send(Msg );
}