利用Java Mail发送邮件的代码详解编程语言

import java.util.Properties; 
  
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeUtility; 
  
import org.apache.commons.lang.StringUtils; 
  
public class MailUtil { 
      
    private MimeMessage message; 
    private Session session; 
    private Transport transport; 
    private String smtpHost = ""; 
    private int smtpPort = 25; 
    private String senderUserName = ""; 
    private String senderPassword = ""; 
  
    public MailUtil(Properties properties, boolean debug) { 
        this.smtpHost = properties.getProperty("mail.smtp.host"); 
        this.smtpPort = Integer.parseInt(properties.getProperty("mail.smtp.port")); 
        this.senderUserName = properties.getProperty("mail.sender.username"); 
        this.senderPassword = properties.getProperty("mail.sender.password"); 
        session = Session.getInstance(properties); 
        session.setDebug(debug);//开启后有调试信息 
        message = new MimeMessage(session); 
    } 
  
    /** 
     * 发送邮件 
     * 
     * @param subject        邮件主题 
     * @param mailBody       邮件内容 
     * @param senderNickName 发件人NickName 
     * @param receiveUser    收件人地址 
     * @param ccReceiveUser  抄送地址 
     * @param bccReceiveUser 密送地址 
     */ 
    public void sendEmail(String subject, String mailBody, String senderNickName, 
                          String receiveUser, String ccReceiveUser, String bccReceiveUser, Boolean isHtmlFormat) { 
        try { 
            // 发件人 
            InternetAddress from = null; 
            if (StringUtils.isEmpty(senderNickName)) { 
                from = new InternetAddress(senderUserName); 
            } else { 
                from = new InternetAddress(MimeUtility.encodeWord(senderNickName) + " <" + senderUserName + ">"); 
            } 
            message.setFrom(from); 
  
            // 收件人 
            InternetAddress to = new InternetAddress(receiveUser); 
            message.setRecipient(Message.RecipientType.TO, to); 
  
            //抄送人 
            if (!StringUtils.isEmpty(ccReceiveUser)) { 
                InternetAddress cc = new InternetAddress(ccReceiveUser); 
                message.setRecipient(Message.RecipientType.CC, cc); 
            } 
  
            //密送人 
            if (!StringUtils.isEmpty(bccReceiveUser)) { 
                InternetAddress bcc = new InternetAddress(bccReceiveUser); 
                message.setRecipient(Message.RecipientType.BCC, bcc); 
            } 
  
            message.setSubject(subject); 
            String content = mailBody.toString(); 
  
            if (isHtmlFormat) { 
                message.setContent(content, "text/html;charset=UTF-8"); 
            } else { 
                message.setContent(content, "text/plain;charset=UTF-8"); 
            } 
            message.saveChanges(); 
            transport = session.getTransport("smtp"); 
            transport.connect(smtpHost, smtpPort, senderUserName, senderPassword); 
            transport.sendMessage(message, message.getAllRecipients()); 
  
  
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            if (transport != null) { 
                try { 
                    transport.close(); 
                } catch (MessagingException e) { 
                    e.printStackTrace(); 
                } 
            } 
        } 
    } 
  
    public static void main(String[] args) { 
        Properties properties = new Properties(); 
        properties.setProperty("mail.smtp.host", "smtp.sina.com"); 
        properties.setProperty("mail.smtp.port", "25"); 
        properties.setProperty("mail.smtp.auth", "true"); 
        properties.setProperty("mail.sender.username", "XXXXXXXXX"); 
        properties.setProperty("mail.sender.password", "XXXXXXXXX"); 
        MailUtil mailUtil = new MailUtil(properties, false); 
          
    } 
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10618.html

(0)
上一篇 2021年7月19日 10:21
下一篇 2021年7月19日 10:21

相关推荐

发表回复

登录后才能评论