java邮件发送的简单实现,使用javamail通过smtp协议发信

1.通过javamail实现
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SimpleAliDMSendMail {
    private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
    private static final int ALIDM_SMTP_PORT = 25;

    public static void main(String[] args) throws MessagingException {
        // 配置发送邮件的环境属性
        final Properties props = new Properties();
        // 表示SMTP发送邮件,需要进行身份验证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", ALIDM_SMTP_HOST);
        props.put("mail.smtp.port", ALIDM_SMTP_PORT);   
        // 如果使用ssl,则去掉使用25端口的配置,进行如下配置, 
        // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        // props.put("mail.smtp.socketFactory.port", "465");
        // props.put("mail.smtp.port", "465");


        // 发件人的账号
        props.put("mail.user", "***");
        // 访问SMTP服务时需要提供的密码
        props.put("mail.password", "***");

        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(
                props.getProperty("mail.user"));
        message.setFrom(form);

        // 设置收件人
        InternetAddress to = new InternetAddress("***");
        message.setRecipient(MimeMessage.RecipientType.TO, to);

        // 设置邮件标题
        message.setSubject("测试邮件");
        // 设置邮件的内容体
        message.setContent("测试的HTML邮件", "text/html;charset=UTF-8");

        // 发送邮件
        Transport.send(message);
    }
}


2.使用Apache Commons-email组件发送邮件

   
   
commons-email是apache提供的一个开源的API,是对javamail的封装,
因此在使用时要将javamail.jar加到 class path中,
主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment四个类。
 
SimpleEmail:发送简单的email,不能添加附件
MultiPartEmail:文本邮件,可以添加多个附件
HtmlEmail:HTML格式邮件,同时具有MultiPartEmail类所有“功能”
EmailAttchment:附件类,可以添加本地资源,也可以指定网络上资源,在发送时自动将网络上资源下载发送。

 
发送基本文本格式邮件:
SimpleEmail email = new SimpleEmail();
//smtp host 
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("ttest@163.com", "John Doe");
//发送人
email.setFrom("me@163.com", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setMsg("This is a simple test of commons-email");
//发送
email.send();



发送文本格式,带附件邮件:
//附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e:\\1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
//smtp host 
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("test@163.com", "John Doe");
//发送人
email.setFrom("me@163.com", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//发送
email.send();


 
发送HTML格式带附件邮件:
//附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e:\\1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
HtmlEmail email = new HtmlEmail ();
//smtp host 
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("test@163.com", "John Doe");
//发送人
email.setFrom("me@163.com", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setHtmlMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//发送
email.send();



下面提供一个完整的程序示例:
package test

import org.apache.commons.mail.*;

public class SendEMail
{
    public static void main ( String[] arg ) throws Exception
    {
        SimpleEmail email = new SimpleEmail ( );

        // smtp host
        email.setHostName ( "smtp.163.com" );
        // 登陆邮件服务器的用户名和密码
        email.setAuthentication ( "test", "123456" );
        // 接收人
        email.addTo ( "test@yahoo.com.cn", "test" );
        // 发送人
        email.setFrom ( "test@163.com", "测试者" );
        // 标题
        email.setSubject ( "Test message" );
        // 邮件内容
        email.setMsg ( "This is a simple test of commons-email 测试......" );
        // 发送
        email.send ( );
        
        System.out.println ( "发送成功 Send email successful!" );

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值