工程目录如下:

 

1、准备javaMail需要的两个Jar包:mail.jar、activation.jar,然后add to build path

2、QQ邮箱开启SMTP服务,开启后,它会给你一串授权码

 

 完整代码如下所示:

import java.security.GeneralSecurityException;
import java.util.Properties;

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

import com.sun.mail.util.MailSSLSocketFactory;


public class HelloMail {

    public static void main(String[] args) throws GeneralSecurityException {
        
          // 收件人电子邮箱
          String to = "xxxxx@qq.com";
     
          // 发件人电子邮箱
          String from = "xxxxx@qq.com";
     
          // 获取系统属性
          Properties properties = new Properties();
          
          //发送邮件协议
          properties.setProperty("mail.transport.protocol", "SMTP");
     
          // 设置邮件服务器
          properties.setProperty("mail.host", "smtp.qq.com");
          
          // 设置邮件服务器端口
          properties.setProperty("mail.smtp.port", "465");
          
          //开启SSL加密:QQ邮箱需要
          MailSSLSocketFactory sf = new MailSSLSocketFactory();
          sf.setTrustAllHosts(true);
          properties.put("mail.smtp.ssl.enable", "true");
          properties.put("mail.smtp.ssl.socketFactory", sf);
          
          // 设置邮件服务器是否需要登录认证
          properties.setProperty("mail.smtp.auth", "true");
          
          
          // 验证账号及密码,密码需要是第三方授权码
          Authenticator auth = new Authenticator() {
          public PasswordAuthentication getPasswordAuthentication(){
             return new PasswordAuthentication("xxxxx@qq.com", "这里添授权码");
                     }
                 };
     
          // 获取默认session对象
          Session session = Session.getInstance(properties,auth);
     
          try{
             // 创建默认的 MimeMessage 对象
             MimeMessage message = new MimeMessage(session);
     
             // Set From: 头部头字段
             message.setFrom(new InternetAddress(from));
     
             // Set To: 邮件接收人
             message.addRecipient(Message.RecipientType.TO,
                                      new InternetAddress(to));
     
             // Set Subject: 头部头字段
             message.setSubject("This is the Subject Line!");
     
             // 设置消息体
             message.setText("This is actual message");
     
             // 发送消息
             Transport.send(message);
             System.out.println("Sent message successfully....");
          }catch (MessagingException mex) {
             mex.printStackTrace();
          }
        
    }
    
}

 

 运行成功,然后qq邮箱就收到了邮件

 

 

带附件发送:

 

import java.io.File;
import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.MailSSLSocketFactory;

/*
 * 带附件的邮件
 */
public class MailAttach {

    public static void main(String[] args) throws GeneralSecurityException {
        
          // 收件人电子邮箱
          String to = "XXXXXXX";    //也可以的
     
          // 发件人电子邮箱
          String from = "XXXXXXX@qq.com";
     
          // 获取系统属性
          Properties properties = new Properties();
          
          //发送邮件协议
          properties.setProperty("mail.transport.protocol", "SMTP");
     
          // 设置邮件服务器
          properties.setProperty("mail.host", "smtp.qq.com");
          
          // 设置邮件服务器端口
          properties.setProperty("mail.smtp.port", "465");
          
          //开启SSL加密:QQ邮箱需要
          MailSSLSocketFactory sf = new MailSSLSocketFactory();
          sf.setTrustAllHosts(true);
          properties.put("mail.smtp.ssl.enable", "true");
          properties.put("mail.smtp.ssl.socketFactory", sf);
          
          // 设置邮件服务器是否需要登录认证
          properties.setProperty("mail.smtp.auth", "true");
          
          
          // 验证账号及密码,密码需要是第三方授权码
          Authenticator auth = new Authenticator() {
          public PasswordAuthentication getPasswordAuthentication(){
             return new PasswordAuthentication("XXXXXXX@qq.com", "授权码");
                     }
                 };
     
          // 获取默认session对象
          Session session = Session.getInstance(properties,auth);
     
          try{
             // 创建默认的 MimeMessage 对象
             MimeMessage message = new MimeMessage(session);
     
             // Set From: 头部头字段
             message.setFrom(new InternetAddress(from));
     
             // Set To: 邮件接收人
             message.addRecipient(Message.RecipientType.TO,
                                      new InternetAddress(to));
     
             // Set Subject: 主题名称
             message.setSubject("This is the Subject Line!");
             
            // 创建消息部分 
             BodyPart messageBodyPart = new MimeBodyPart(); 
             
             // 消息内容
             messageBodyPart.setText("TEST/TEST"); 
           
             // 创建多重消息 
             Multipart multipart = new MimeMultipart(); 
            
             // 设置文本消息部分 
             multipart.addBodyPart(messageBodyPart); 
            
             // 附件部分 
             messageBodyPart = new MimeBodyPart(); 
             
             //把文件,添加到附件1中
             //数据源
             DataSource source = new FileDataSource(new File("附件路径,比如:D:/test.zip"));
             //设置第一个附件的数据
             messageBodyPart.setDataHandler(new DataHandler(source));
             //设置附件的文件名
             messageBodyPart.setFileName("file1.zip");
             multipart.addBodyPart(messageBodyPart);
             message.setContent(multipart);
     
             // 发送消息
             Transport.send(message);
             System.out.println("Sent message successfully....");
          }catch (MessagingException mex) {
             mex.printStackTrace();
          }
        
        
    }
    
    
}

 

 posted on 2018-04-08 16:03  布鲁布鲁sky  阅读(1761)  评论(1编辑  收藏  举报