zoukankan      html  css  js  c++  java
  • java mail发送邮件

    主要是参考

    http://docs.oracle.com/javaee/6/api/javax/mail/Session.html#getInstance(java.util.Properties)

    https://www.tutorialspoint.com/javamail_api/javamail_api_authentication.htm

    https://www.javatpoint.com/example-of-sending-email-using-java-mail-api

    package com.xmal.dms.util;
    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    
    import org.apache.commons.lang.StringUtils;
    import org.springframework.mail.javamail.MimeMessageHelper;
    
    import javax.activation.*;
    public class MailUtils { 
    	public static void sendMail(List<String> recipientsList,String content,String Subject) throws AddressException {
           // Recipient's email ID needs to be mentioned.
    	  InternetAddress[] recipientsArr = new InternetAddress[recipientsList.size()];
    	  recipientsArr = InternetAddress.parse(StringUtils.join(recipientsList,","));
    	   // Sender's email ID needs to be mentioned
    	   String user = "myzonedj@163.com";
    	   String passWord =  "adventure/2011/";
    
    	   // Assuming you are sending email from localhost
    	   String host = "smtp.163.com";
    
    	   // Get system properties
    	   Properties properties = System.getProperties();
    
    	   // Setup mail server
    	   properties.setProperty("mail.smtp.host", host);
    	    
    	   //setup password
    	   properties.setProperty("mail.smtp.auth", "true");  
     
    	   // Get the default Session object.
    	   Session session = Session.getDefaultInstance(properties,  
    	    	    new javax.mail.Authenticator() {  
    	    	      protected PasswordAuthentication getPasswordAuthentication() {  
    	    	    return new PasswordAuthentication(user,passWord);  
    	    	      }  
    	       });  
    
    	   try {
    	      // Create a default MimeMessage object.
    	      MimeMessage message = new MimeMessage(session);
    	      MimeMessageHelper messageHelper = new MimeMessageHelper(message,"UTF-8");
    	      
    	      // Set To: header field of the header.
    	      messageHelper.setTo(recipientsArr);
    	      
    	      // Set From: header field of the header.
    	      messageHelper.setFrom(new InternetAddress(user));
    
          // Set Subject: header field messageHelper.setSubject(Subject); // Send the actual HTML message, as big as you like //message.setContent(content, "text/html"); messageHelper.setText(content, true); // message.setText(content); // Send message Transport.send(messageHelper.getMimeMessage());; System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }

      

  • 相关阅读:
    promethus监控JVM jar包
    ubuntu中文乱码解决办法
    IT焦躁中的赤子青年
    ftp neo4j http kafka搭建
    查看python脚本执行过程
    解决coredns-7f9c544f75-2sjs2一直处于ContainerCreating
    番茄工作法
    数据库的性能优化
    MyBatis
    CentOS下安装JDK,Tomcat,Redis,Mysql,及项目发布
  • 原文地址:https://www.cnblogs.com/hong2016/p/7451336.html
Copyright © 2011-2022 走看看