email 邮件发送配置

maven配置:

<dependency>
     <groupId>javax.mail</groupId>
     <artifactId>mail</artifactId>
     <version>1.4.5</version>
    </dependency>
    <dependency>

代码:

package com.zhou.core.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
 * 邮件发送
 * @author 
 *
 */
public class SendEmail {
    public static void main(String[] args) {
        final String fromMail = "to@163.com";
        String toMail =  "from@163.com";
        String host = "smtp.163.com";
        //邮件服务器设置
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", host);
        props.setProperty("mail.debug", "true");
        props.setProperty("mail.smtp.auth", "true");

        //获取默认session对象
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromMail, "123456");//邮件账户密码设置
                } 
        };
        Session session = Session.getDefaultInstance(props,auth);
        try {
            MimeMessage mm = new MimeMessage(session);
            //发送方设置
            mm.setFrom(new InternetAddress(fromMail));
            //接收方设置
            mm.addRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
            //头部头字段
            mm.setSubject("this is the subject line!", "utf-8");
            //消息体
            mm.setText("this is actual message", "utf-8");

            Transport.send(mm);
            System.out.println("set message successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值