您的位置:首页 > 编程语言 > Java开发

java发邮件

2015-08-13 08:54 579 查看
package com;

import java.util.Date;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.Address;

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;

public class SendEmail2

{

   public static void main(String [] args)

   {

      // 收件人电子邮箱

      String to = "1215402223@qq.com";

      String tochao = "zhangweibin@yunxinsoft.cn";

      // 发件人电子邮箱

      String from = "1215402223@qq.com";

      // 指定发送邮件的主机为 localhost

      String host = "smtp.qq.com";  //QQ 邮件服务器

      // 获取系统属性

      Properties properties = System.getProperties();

      // 设置邮件服务器

      properties.setProperty("mail.smtp.host", host);//

      //mail.smtp.host  QQ

      properties.put("mail.smtp.auth", "true");

      // 获取默认session对象

      Session session = Session.getDefaultInstance(properties,new Authenticator(){
   public PasswordAuthentication getPasswordAuthentication()
   {
    return new PasswordAuthentication("你的邮箱", "邮箱密码"); //发件人邮件用户名、密码
   }
  });

      try{

         // 创建默认的 MimeMessage 对象

         MimeMessage message = new MimeMessage(session);

         // Set From: 头部头字段

         message.setFrom(new InternetAddress(from));

 

         // 发件人地址

         InternetAddress[] address = { new InternetAddress(to) };

      // 发件人地址

         InternetAddress[] addresschao = { new InternetAddress(tochao) };

         

          //发送

         message.setRecipients(Message.RecipientType.TO, address);

         //抄送

         message.setRecipients(Message.RecipientType.CC, addresschao);

         

         

         

         

         

         //定时发送

         message.setSentDate(new Date());

         

 

         // Set Subject: 头字段

         message.setSubject("头字段");

 

         // 创建消息部分

         BodyPart messageBodyPart = new MimeBodyPart();

 

         // 消息

         messageBodyPart.setText("文本内容");

        

         // 创建多重消息

         Multipart multipart = new MimeMultipart();

 

         // 设置文本消息部分

         multipart.addBodyPart(messageBodyPart);

 

         

         // 添加附件部分

         messageBodyPart = new MimeBodyPart();

         String filename = "F:/dataDictionary.json";

         DataSource source = new FileDataSource(filename);

         messageBodyPart.setDataHandler(new DataHandler(source));

         messageBodyPart.setFileName(filename);

         multipart.addBodyPart(messageBodyPart);

 

         

         // 发送完整消息

         message.setContent(multipart );

         

        

         // 发送消息

         Transport.send(message);

         System.out.println("发送成功");

      }catch (MessagingException mex) {

         mex.printStackTrace();

      }

   }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  邮件