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(); } } }

      

  • 相关阅读:
    【linux】统计文件夹中文件行数
    【python】__import__
    【深度学习】吴恩达网易公开课练习(class2 week1 task2 task3)
    【python】多进程共享变量Manager
    【python】随机数用法
    【深度学习】吴恩达网易公开课练习(class2 week1)
    DIY博客园的博客皮肤
    电子图书馆(仿百度文库)
    查看html元素绑定的事件与方法的利器
    HyPer: A Hybrid OLTP&OLAP Main Memory Database System Based on Virtual Memory Snapshots
  • 原文地址:https://www.cnblogs.com/hong2016/p/7451336.html
Copyright © 2011-2022 走看看