Java写邮件发送

发送激活邮件

 

电子邮箱

邮件服务器

邮件协议

Smtp邮件发送

Pop3邮件接收

Imap邮件接收

 

邮件发送的全过程

搭建

 

 

 

搭建邮箱服务器

点击安装,注意:一定要安装在C

 

2 修改域名

工具--->服务器设置---->aigouwu.com

添加几个邮箱,用来接收邮件,

 

3 注册账号

账号---->新建账号

 

 

安装客户端软件(接收和发送邮件)

 

Outlookfoxmail

新建邮箱用户,

 

需要注意的是

*如果添加的是sohusina等公网的邮箱则接收服务器和发送服务器地址都是公网上的,

配置自己电脑邮件服务器的邮箱用户则将接收服务器地址和发送服务器地址都是    localhost

配置

配置发送邮件服务器

Localhost

配置接收邮件服务器

Localhost

 

编码实现

复制放到lib中,

删除javaee5.0里面的activation.jar mail.jar

 

Add jars--->使用WINRAR打开----->删除activation.jarmail.jar

 

编码:

package com.hbliti.shop.utils;

 

import java.util.Properties;

 

import javax.mail.Authenticator;

import javax.mail.Message;

import javax.mail.Message.RecipientType;

import javax.mail.MessagingException;

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.MimeMessage;

 

/**

* 发送验证邮件工具类

* @authorsonwing_for@sina.com*

*/

publicclass MailUtils {

    /**

     *

     * @param to:收件人

     * @param code:激活码

     */

    publicstaticvoid sendMail(String to , String code){

        /**

         * 发送邮件的步骤:

         * 1.获得一个Session对象

         * 2.创建一个代表邮件的对象Message

         * 3.发送邮件Transport

         *

         */

        Properties props=new Properties();

        props.setProperty("mail.host", "localhost");

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

        //获得连接对象

        //Authentication:通过用户名和密码通过认证

        //props:邮件服务器地址smtp协议

        //Authentication:使用这个邮箱名和密码来向用户发送邮件

        Session session=Session.getInstance(props, new Authenticator(){

 

            @Override

            protected PasswordAuthentication getPasswordAuthentication() {

                // TODO Auto-generated method stub

                returnnew PasswordAuthentication("service@94aigouwu.com", "111");

            }

            

        });

        //2.创建一个邮件对象

        Message message=new MimeMessage(session);

        

        try {

            //邮件的发件人

            message.setFrom(new InternetAddress("service@94aigouwu.com"));

            //邮件的收件人

            message.addRecipient(RecipientType.TO, new InternetAddress(to));

            //邮件的主题

            message.setSubject("来自94aigouwu网的官方激活邮件");

            //邮件的内容

            message.setContent("<h1>来自94aigouwu网的官方激活邮件,点下面链接来激活操作</h1><h3><a href='http://localhost:8080/94aigouwu/user_active.action?code="+code+"'>http://localhost:8080/94aigouwu/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");

            //发送邮件

            Transport.send(message);

        } catch (AddressException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (MessagingException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        

    }

    

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sonwing_for

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值