JavaMail的学习和使用

JavaMail的学习和使用

import java.io.IOException;
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.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

/**
*
* @author ASUS
*
* JavaMail第一例
*
*/
public class Demo1 {

@Test
public void fun() throws AddressException, MessagingException {
    /*
     * 1.得到Session
     */
    Properties props = new Properties();

    props.setProperty("mail.host", "smtp.163.com");// 设置邮件服务器地址
    props.setProperty("mail.smtp.auth", "true");// 设置邮件服务器是否需要登录认证

    // 创建认证器
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("xxx",
                    "******");// 指定用户名和密码
        }
    };
    // 获取Session对象
    Session session = Session.getInstance(props, auth);

    /*
     * 2.创建MimeMessage
     */
    // 创建邮件对象
    /*
     * RecipientType.TO RecipientType.CC抄送,收件人收到的邮件信息中显示抄送对象
     * RecipientType.BCC密送,收件人收到的邮件信息中不显示密送对象
     */
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("xxx@163.com"));// 设置发件人
    msg.setRecipient(RecipientType.TO, new InternetAddress(
            "xxx@qq.com"));// 设置收件人

    // Set Subject: 头部头字段,邮件主题
    msg.setSubject("这是一封测试邮件!");

    // 设置消息体
    // 发送普通文字邮件
    // msg.setText("这是一封用来测试JavaMail的垃圾邮件");

    // 发送 HTML 消息, 可以插入html标签
    msg.setContent("<h1>This is actual message</h1>", "text/html");
    // msg.setContent("这是一封用来测试JavaMail的垃圾邮件", "text/html;charset=utf-8");

    /*
     * 3.发送邮件
     */
    Transport.send(msg);
    System.out.println("Sent message successfully....");

}

/**
 * 发送带附件的邮件
 * 
 * @throws AddressException
 * @throws MessagingException
 * @throws IOException
 */
@Test
public void fun2() throws AddressException, MessagingException, IOException {
    /*
     * 1.得到Session
     */
    Properties props = new Properties();

    props.setProperty("mail.host", "smtp.163.com");// 设置邮件服务器地址
    props.setProperty("mail.smtp.auth", "true");// 设置邮件服务器是否需要登录认证

    // 创建认证器
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("xxx",
                    "******");// 指定用户名和密码
        }
    };
    // 获取Session对象
    Session session = Session.getInstance(props, auth);

    /*
     * 2.创建MimeMessage
     */
    // 创建邮件对象
    /*
     * RecipientType.TO RecipientType.CC抄送,收件人收到的邮件信息中显示抄送对象
     * RecipientType.BCC密送,收件人收到的邮件信息中不显示密送对象
     */
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("xxx@163.com"));// 设置发件人
    msg.setRecipient(RecipientType.TO, new InternetAddress(
            "xxx@qq.com"));// 设置收件人

    // Set Subject: 头部头字段,邮件主题
    msg.setSubject("这是一封测试邮件!用来发送带附件的邮件");

    // 创建消息部分
    BodyPart messageBodyPart1 = new MimeBodyPart();

    // 消息,之前把要发送的文字消息直接设置到msg中, msg.setText("这是一封用来测试JavaMail的垃圾邮件");
    // 现在为了发送带附件的邮件,使用多重消息实现,因为多重消息可以包含多个部件,我们把要发送的每部分装进部件中,即设置各部件的内容
    // 然后设置到多重消息中,最后设置为要发送的message
    messageBodyPart1.setText("This is message body");

    // 创建多重消息
    Multipart multipart = new MimeMultipart();

    // 设置文本消息部分,将部件加入多重消息
    multipart.addBodyPart(messageBodyPart1);

    // 附件部分
    // BodyPart messageBodyPart2 = new MimeBodyPart();
    // String filename = "file.txt";
    // DataSource source = new FileDataSource(filename);
    // messageBodyPart2.setDataHandler(new DataHandler(source));
    // messageBodyPart2.setFileName(filename);
    // multipart.addBodyPart(messageBodyPart2);

    // 附件部分
    MimeBodyPart messageBodyPart3 = new MimeBodyPart();
    // 为部件指定附件,是附件的内容
    messageBodyPart3.attachFile("C:/test.jpg");
    // 指定附件名称,必须得单独指定,并不会从附件内容中自动提取
    //使用MimeUtility.encodeText()对中文进行编码,解决附件名称中文乱码问题
    //messageBodyPart3.setFileName(MimeUtility.encodeText("测试.jpg"));
    messageBodyPart3.setFileName("test.jpg");
    // 将部件加入多重消息
    multipart.addBodyPart(messageBodyPart3);

    // 发送完整消息
    msg.setContent(multipart);

    /*
     * 3.发送邮件
     */
    Transport.send(msg);
    System.out.println("Sent message successfully....");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值