javaMail learning (3) - use javaMail to send simple emails to the Easy Mail account

Not much to say, let's go directly to the code:

package com.wjl.mail.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Tools for sending emails
 * Use eyoumailserver to send emails from internal accounts
 */
/*
javax.mail.Session: contextual information, such as the server's host name, port number, protocol name, etc.  
javax.mail.Message: The mail model, the medium for sending and receiving mail, encapsulates the information of the mail, such as sender, recipient, mail title, mail content, etc.  
javax.mail.Transport: connect to the mail SMTP server and send mail  
javax.mail.Store: connect mail POP3, IMAP server, receive mail
*/
public class MailUtils2 {
	private static String userName = "[email protected]";//The person who sent the email
	private static String password = "123456";//The password of the account that sends the email
	private static String userName2 = "[email protected]";//The person who receives the mail
	private static String port = "25";//465、587
	/**
	 * This method is used to send mail
	 * @param to: who to email
	 * **/
	public static void sendMain(String to) throws AddressException, MessagingException{
		//1. Create a connection object and connect to the mailbox server
		Properties props = new Properties();
		//Enable debug debugging  
        props.setProperty("mail.debug", "true");  
        //stmp server needs to be authenticated, that is, the verification of username and password, so that it can pass the verification
        props.setProperty("mail.smtp.auth", "true");
        // send mail protocol name  
        props.setProperty("mail.transport.protocol", "smtp");  
        //Set the mail server hostname  
        props.setProperty("mail.host", "127.0.0.1");//本地
        //Set the port number (this configuration can be written or not)
        props.setProperty("mail.smtp.port", port);
		
		//Authenticator: Authentication information
		Session session = Session.getInstance(props, new Authenticator(){
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName,password);//Use it to send emails to other accounts
			}
		});
		
		//2, create the mail object
		Message message = new MimeMessage(session);
		//2.1 Set the sender
		message.setFrom(new InternetAddress(userName));
		//2, 2 set the recipient
		message.setRecipient(RecipientType.TO, new InternetAddress(to));
		//2.3 Subject of the email
		message.setSubject("Test message");
		//2.4 The body of the email (ie the content of the email)
		message.setContent("Test mail: mail from [email protected]","text/html;charset=utf-8");
		
		//3. Send email
		Transport trans = session.getTransport();
		//connect to mail server
        trans.connect(userName, password);  
		//send email
        trans.sendMessage(message, message.getAllRecipients());  
        //close the connection
        trans.close();
        
        //Transport.send(message); (both ways are possible)
		
		System.out.println("Successfully sent");
	}
	
	public static void main(String[] args) {
		try {
			sendMain(userName2);
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}
}

After successful execution, use Foxmail to view the mail.

Indicates that the email was sent successfully.

 

Possible problems :

Error when executing code: java.lang.NoClassDefFoundError: com/sun/mail/util/BEncoderStream

The reason is that the version of the jar package is not uniform, and there is a conflict between javaMail and the package in Java EE 5 Libraries/javaee.jar/mail.

Workaround: Replace Java EE 5 Libraries with Java EE 6 Libraries

Libraries replace:

Right-click on the selected project---->Properties---->Java Build Path---->Add Library, as shown in the figure:

Select the default "MyEclipse Libraries".

Check "Java EE 6 Libraries". Then Finish.

At this point, there are two Libraries in Libraries, one is 5 and the other is 6, delete the Java EE 5 Libraries, and then confirm.

Execute the code again, and there will be no more errors.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326384758&siteId=291194637