java 发邮件demo

package com.zsjz.utils.tools;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 org.apache.log4j.Logger;

public class MailSender {
    private static Logger LOGGER = Logger.getLogger(MailSender.class);
    
    public static boolean sendTextMail(MailInfo mailInfo){
        // 判断是否需要身份认证
        MyAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            // 如果需要身份认证,则创建一个密码验证器
            authenticator = new MyAuthenticator(mailInfo.getUserName(),
                    mailInfo.getPassword());
        }
        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session sendMailSession = Session
                .getDefaultInstance(pro, authenticator);
        try {
            // 根据session创建一个邮件消息
            Message mailMessage = new MimeMessage(sendMailSession);
            // 创建邮件发送者地址
            Address from = new InternetAddress(mailInfo.getFromAddress());
            // 设置邮件消息的发送者
            mailMessage.setFrom(from);
            // 创建邮件的接收者地址,并设置到邮件消息中
            Address to = new InternetAddress(mailInfo.getToAddress());
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            // 设置邮件消息的主题
            mailMessage.setSubject(mailInfo.getSubject());
            // 设置邮件消息发送的时间
            mailMessage.setSentDate(new Date());
            // 设置邮件消息的主要内容
            String mailContent = mailInfo.getContent();
            mailMessage.setText(mailContent);
            // 发送邮件
            Transport.send(mailMessage);
            return true;
        } catch (MessagingException ex) {
            LOGGER.error("发送文本邮件失败", ex);
            //throw new KuHuBaoException("发送文本邮件失败",ex);
        }
        return false;
    }
    public static boolean sendHtmlMail(MailInfo mailInfo){
        // 判断是否需要身份认证
        MyAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        // 如果需要身份认证,则创建一个密码验证器
        if (mailInfo.isValidate()) {
            authenticator = new MyAuthenticator(mailInfo.getUserName(),
                    mailInfo.getPassword());
        }
        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session sendMailSession = Session
                .getDefaultInstance(pro, authenticator);
        try {
            // 根据session创建一个邮件消息
            Message mailMessage = new MimeMessage(sendMailSession);
            // 创建邮件发送者地址
            Address from = new InternetAddress(mailInfo.getFromAddress());
            // 设置邮件消息的发送者
            mailMessage.setFrom(from);
            // 创建邮件的接收者地址,并设置到邮件消息中
            Address to = new InternetAddress(mailInfo.getToAddress());
            // Message.RecipientType.TO属性表示接收者的类型为TO
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            // 设置邮件消息的主题
            mailMessage.setSubject(mailInfo.getSubject());
            // 设置邮件消息发送的时间
            mailMessage.setSentDate(new Date());
            // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
            Multipart mainPart = new MimeMultipart();
            // 创建一个包含HTML内容的MimeBodyPart
            BodyPart html = new MimeBodyPart();
            // 设置HTML内容
            html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            // 将MiniMultipart对象设置为邮件内容
            mailMessage.setContent(mainPart);
            // 发送邮件
            Transport.send(mailMessage);
            return true;
        } catch (Exception ex) {
            LOGGER.error("发送HTML文件格式邮件失败", ex);
            ex.printStackTrace();
            //throw new Exception("发送HTML文件格式邮件失败",ex);
        }
        return false;
    }
    //410818649@qq.com
    //[语文]2015-12-06 星期日 家庭作业
    //<a href="http://www.zsjz888.com//zsjz-webapp/upload/file/20151206/20151206164246_829.doc"  target="_blank">[语文]2015-12-06 星期日 作业学习资料下载</a>
    
    public static void main(String[] args) {
        MailSender.sendHtml("774578770@qq.com","[语文]2015-12-06 星期日 家庭作业","你好");
        
        
    }
    
    public static boolean sendHtml(String email,String title,String content) {
        // 设置邮件服务器信息
                MailInfo mailInfo = new MailInfo();
                mailInfo.setMailServerHost("smtp.qq.com");
                mailInfo.setMailServerPort("25");
                mailInfo.setValidate(true);
                // 邮箱用户
                mailInfo.setUserName("1651886967@qq.com");
                // 邮箱密码
                mailInfo.setPassword("*********");//要开通邮箱的smtp服务,用那上面的密码
                // 发件人邮箱
                mailInfo.setFromAddress("1651886967@qq.com");
                // 收件人邮箱
                mailInfo.setToAddress(email);
                // 邮件标题
                mailInfo.setSubject(title);
                // 邮件内容

                
                StringBuffer buffer = new StringBuffer();

                buffer.append(content);
                mailInfo.setContent(buffer.toString());

                // 发送文体格式
                try {
                    if(MailSender.sendHtmlMail(mailInfo)){
                        return true;
                    }
                    // 发送html格式
                    // SimpleMailSender.sendHtmlMail(mailInfo);
                    System.out.println("邮件发送完毕");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值