This article explains the detailed process of sending java emails for your reference. The specific content is as follows
1. Email Agreement
Emailed by:SMTP (Simple Mail Transport Protocal)
Email reception protocol: pop3 (Post Office Protocal 3)
Emails can also be received by sending new IMAP protocols.
(Interact with the server step by step)
SMTP:
2. The process of sending and receiving emails:
Generally speaking, smtp and pop3 are two servers (hosts).
The port of Smtp mail is 25.
The POP3 port is 110.
Email example
1) Install foxmail:
2) When sending an email, base64 encoding of the user name and password
//Base64 encoding of username and password @Test public void base64(){ String name = "wj_leaf12345"; String pwd = "1qaz2wsx"; BASE64Encoder en = new BASE64Encoder(); name = en.encode(name.getBytes()); pwd = en.encode(pwd.getBytes()); System.err.println(name); System.err.println(pwd); }3) Send emails through java code
Send emails in java, you must import a new package
mail.jar core package for sending emails
activation.jar encrypts users and passwords.
There are three core classes in mail.jar:
Javax.mail.Session refers to a session with a mail server. There is only one in the entire project.
Javax.mail.Message (interface) - prepare to send data information.
MimeMessage - You can set the type of data information.
Transport It has a method to send a Message.
Step 1: Import two jar packages
Step 2: Send a simple email
public void sendMail() throws Exception{ //Step 1: Declare the properties object to put information Properties prop = new Properties(); //Set which server to connect to 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 = new Authenticator() { //This access object that returns the user and password public PasswordAuthentication getPasswordAuthentication() { PasswordAuthentication pa = new PasswordAuthentication("aaa", "sss"); return pa; } }; ////Step 2: Get the Session object Session session = Session.getDefaultInstance(prop,auth); //Set the debug mode of session session.setDebug(true); //Step 3: Declare the information MimeMessage mm1 = new MimeMessage(session); //Step 4: Set the sender email Address from = new InternetAddress("[email protected]"); mm1.setFrom(from); //Step 5: Set the recipient mm1.setRecipient(RecipientType.TO,new InternetAddress("[email protected]")); mm1.setRecipient(RecipientType.CC, new InternetAddress("[email protected]")); mm1.setRecipient(RecipientType.BCC, new InternetAddress("[email protected]")); //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, try again 3333", "text/plain;charset=UTF-8"); //Step 7: Transport.send(mm1); }Step 3: V send emails with hyperconnection
mm1.setSubject("This is the email sent in Java sfasdf3"); mm1.setContent("Hello, this is the 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 of the symbol
public void sendFile() throws Exception{ Properties p = new Properties(); p.setProperty("mail.host","smtp.163.com"); p.setProperty("mail.smtp.auth","true"); Session s = Session.getDefaultInstance(p,new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("ww", "123"); } }); s.setDebug(true); //Declare MimeMessage MimeMessage msg = new MimeMessage(s); msg.setFrom(new InternetAddress("[email protected]")); msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); msg.setSubject("picture"); //Step 1: Declare Multiprocessing Part MimeMultipart mm = new MimeMultipart(); //Step 2: Declare MimeBodyPart body1 = new MimeBodyPart(); //Step 3: Set the character DataSource ds = new FileDataSource(new File("./img/a.jpg")); DataHandler dh = new DataHandler(ds); body1.setDataHandler(dh); //The name must be set body1.setFileName(MimeUtility.encodeText("Beauty.jpg")); MimeBodyPart body2 = new MimeBodyPart(); //Step 3: Set the character DataSource ds2 = new FileDataSource(new File("./img/b.jpg")); DataHandler dh2 = new DataHandler(ds2); body2.setDataHandler(dh2); //The name must be set body2.setFileName(MimeUtility.encodeText("Beauty 2.jpg")); MimeBodyPart body3 = new MimeBodyPart(); //Step 3: Set the character DataSource ds3 = new FileDataSource(new File("./img/m.mp3")); DataHandler dh3 = new DataHandler(ds3); body3.setDataHandler(dh3); //The name must be set body3.setFileName(MimeUtility.encodeText("end of the century.mp3")); //Add body1 to mm mm.addBodyPart(body1); mm.addBodyPart(body2); mm.addBodyPart(body3); msg.setContent(mm); //Send Transport.send(msg); }The above is all about this article, I hope it will be helpful to everyone's learning.