Java implementation sending e-mail

import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public  class SendEmailUtil {
    
    public static boolean sendMail(String emailAddress, String emailMsg) {
        
        // Who sent (sender) 
        String from = "" ;
        
        // To whom 
        String to = emailAddress;
        
        // sender's user name and password (email log in) 
        Final String username = ""; // here to fill in the name of the mailbox to send the 
        Final String password = ""; // here to fill in the login email password
 
        // definition of the properties object, environment information provided 
        the Properties properties = new new the Properties ();
        
        /*
         * Mail.smtp.host: Specifies the host name of the mail server connection. Such as: 163 E-mail to fill smtp.163.com
         * If the test locally, then you need to install smtp server locally
         */
        properties.setProperty("mail.smtp.host", "smtp.163.com");
        
        // mail.smtp.auth: Specifies whether the client would like to submit a verification message server 
        properties.setProperty ( "mail.smtp.auth", "to true" );
        
        /*
         * Mail.transport.protocol: Specifies the e-mail transmission protocol: smtp. smtp: mail; pop3: incoming mail
         * Mail.store.protocol: Specifies the mail receiving protocol
         */
        properties.setProperty("mail.transport.protocol", "smtp");
        
        // 获取session对象
        Session session = Session.getInstance(properties);
        
        // When set to true, JavaMail AP will be the interaction of its operation and mail server command information is output to the console, for debugging to JavaMail 
        session.setDebug ( to true );
         the try {
            
            // create a message object 
            the MimeMessage the Message = new new the MimeMessage (the session);
            
            // Set the message sender 
            message.setFrom ( new new the InternetAddress (from));
            
            // set the theme of the message sent <Mail title> 
            message.setSubject ( "Send Mail settings" );
            
            // set the message content sent 
            message.setContent (emailMsg, "text / HTML; charset = UTF-. 8" );
            Transport transport=session.getTransport();
            
            // connect to the mail server, "" fill in the e-mail server hostname 
            transport.connect ( "", 25 , username, password);
            transport.sendMessage(message,new Address[]{new InternetAddress(to)});
            transport.close();
            return true;
        } catch (MessagingException e) {
            e.printStackTrace ();
            return false;
        }
    }
    

 

Guess you like

Origin www.cnblogs.com/heyjia/p/11163813.html