邮件的发送

邮件发送
1、配置文件设置

2、发送邮件核心代码
    
    /*
         * 发邮件
         * 准备配置文件!
         */
        // 获取配置文件内容
        Properties props = new Properties();
        props.load(this.getClass().getClassLoader()
                .getResourceAsStream("email_template.properties"));
        String host = props.getProperty("host");//获取服务器主机
        String uname = props.getProperty("uname");//获取用户名
        String pwd = props.getProperty("pwd");//获取密码
        String from = props.getProperty("from");//获取发件人
        String to = form.getEmail();//获取收件人
        String subject = props.getProperty("subject");//获取主题
        String content = props.getProperty("content");//获取邮件内容


        content = MessageFormat.format(content, form.getCode());//替换{0}
        
        Session session = MailUtils.createSession(host, uname, pwd);//得到session
        Mail mail = new Mail(from, to, subject, content);//创建邮件对象
        try {
            MailUtils.send(session, mail);//发邮件!
        } catch (MessagingException e) {
        }


3、工具类
package cn.itcast.mail;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

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

/**
 * 
 * @author itcast 本类只有这么一个方法,用来发邮件!
 */
public class MailUtils {
    public static Session createSession(String host, final String username, final String password) {
        Properties prop = new Properties();
        prop.setProperty("mail.host", host);// 指定主机
        prop.setProperty("mail.smtp.auth", "true");// 指定验证为true

        // 创建验证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        };
        
        // 获取session对象
        return Session.getInstance(prop, auth);
    }
    
    /**
     * 发送指定的邮件
     * 
     * @param mail
     */
    public static void send(Session session, final Mail mail) throws MessagingException,
            IOException {

        MimeMessage msg = new MimeMessage(session);// 创建邮件对象
        msg.setFrom(new InternetAddress(mail.getFrom()));// 设置发件人
        msg.addRecipients(RecipientType.TO, mail.getToAddress());// 设置收件人

        // 设置抄送
        String cc = mail.getCcAddress();
        if (!cc.isEmpty()) {
            msg.addRecipients(RecipientType.CC, cc);
        }

        // 设置暗送
        String bcc = mail.getBccAddress();
        if (!bcc.isEmpty()) {
            msg.addRecipients(RecipientType.BCC, bcc);
        }

        msg.setSubject(mail.getSubject());// 设置主题

        MimeMultipart parts = new MimeMultipart();// 创建部件集对象

        MimeBodyPart part = new MimeBodyPart();// 创建一个部件
        part.setContent(mail.getContent(), "text/html;charset=utf-8");// 设置邮件文本内容
        parts.addBodyPart(part);// 把部件添加到部件集中
        
        ///

        // 添加附件
        List<AttachBean> attachBeanList = mail.getAttachs();// 获取所有附件
        if (attachBeanList != null) {
            for (AttachBean attach : attachBeanList) {
                MimeBodyPart attachPart = new MimeBodyPart();// 创建一个部件
                attachPart.attachFile(attach.getFile());// 设置附件文件
                attachPart.setFileName(MimeUtility.encodeText(attach
                        .getFileName()));// 设置附件文件名
                String cid = attach.getCid();
                if(cid != null) {
                    attachPart.setContentID(cid);
                }
                parts.addBodyPart(attachPart);
            }
        }

        msg.setContent(parts);// 给邮件设置内容
        Transport.send(msg);// 发邮件
    }
}


4、导包


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值