Java mail发送邮件

本文提供使用JavaMail API发送电子邮件的基本示例代码,包括简单的纯文本邮件和带有附件的邮件发送过程。示例展示了如何配置邮件服务器属性、设置收件人及抄送人等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导包

mail.jar
activation.jar


示例代码

package cn.yellowimg.javamail;


import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import javax.mail.Authenticator;
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.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.junit.Test;

public class Demo1 {

    @Test
    public void fun1() throws Exception
    {
        /*
         * 1 得到Session对象 即登陆服务器
         */
        Properties properties = new Properties();
        //设置主机IP
        properties.setProperty("mail.host", "smtp.163.com");
        //设置是否验证
        properties.setProperty("mail.smtp.auth", "true");

        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username","password");
            }
        };
        Session session = Session.getInstance(properties, authenticator);

        /*
         * 2 创建MimeMessage
         */
        MimeMessage msg = new MimeMessage(session);
        //设置发件人 
        msg.setFrom(new InternetAddress("me@163.com"));
        /*
         * 设置收件人
         * RecipientType.TO 发件人直接发于收件人
         * RecipientType.CC 抄送
         * RecipientType.BCC 暗送
         */
        msg.setRecipient(RecipientType.TO,new InternetAddress("to@qq.com"));
        //允许你围观 但会被别人知道
        msg.setRecipient(RecipientType.CC,new InternetAddress("cc@qq.com"));
        //允许你围观 不会被别人知道
        msg.setRecipient(RecipientType.BCC,new InternetAddress("bcc@qq.com"));
        //设置标题
        msg.setSubject("这是来自莫斯科的测试邮件");
        //设置正文以及格式
        msg.setContent("莫名我就喜环你 深深的爱上你","text/html;charset=utf-8");
        /*
         * 3 发送邮件
         */
        Transport.send(msg);
    }
    /*
    *    带有附件的发送邮件
    */
    @Test
    public void fun2() throws Exception
    {
        /*
         * 1 得到Session对象 即登陆服务器
         */
        Properties properties = new Properties();

        properties.setProperty("mail.host", "smtp.163.com");

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

        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username","password");
            }
        };
        Session session = Session.getInstance(properties, authenticator);

        /*
         * 2 创建MimeMessage
         */
        MimeMessage msg = new MimeMessage(session);
        //设置发件人 
        msg.setFrom(new InternetAddress("me163.com"));
        /*
         * 设置收件人
         * RecipientType.TO 发件人直接发于收件人
         * RecipientType.CC 抄送
         * RecipientType.BCC 暗送
         */
        msg.setRecipient(RecipientType.TO,new InternetAddress("to@qq.com"));
        //允许你围观 但会被别人知道
        msg.setRecipient(RecipientType.CC,new InternetAddress("cc@qq.com"));
        //允许你围观 不会被别人知道
        msg.setRecipient(RecipientType.BCC,new InternetAddress("bcc@qq.com"));

        msg.setSubject("这是来自莫斯科的附带文件邮件");


        ///////////////////////////////////////////////////////////////
        /*
         * 
         * 发送邮件带附件
         * 
         */
        //创建一个主体 可以理解为集合 一个容器
        MimeMultipart list = new MimeMultipart();

        MimeBodyPart part1 = new MimeBodyPart();
        //设置正文
        part1.setContent("高圆圆的相片", "text/html;charset=utf-8");

        //附件部分
        MimeBodyPart part2 = new MimeBodyPart();
        part2.attachFile(new File("D:/高圆圆.jpg"));
        //设置附件名字 为了防止乱码需要进行编码
        part2.setFileName(MimeUtility.encodeText("高圆圆.jpg"));

        list.addBodyPart(part1);
        list.addBodyPart(part2);

        msg.setContent(list);
        ///////////////////////////////////////////////////////////////

        /*
         * 3 发送邮件
         */
        Transport.send(msg);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值