纯java代码发送邮件

引入jar包activation.jar
               mail.jar
               commons-email.jar


package com.project.framework.util;


import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
/*import com.project.framework.context.ProjectApplication;*/
import com.project.framework.context.ProjectContext;

/**
 * 描述:电子邮件发送接口 目前支持文本格式、HTML格式的内容形式
 *
 * @version
 * @author *ace
 */
public class MailUtils{
   
    public ProjectContext pct;

    private static String username;

    private static String password;
    private static JavaMailSenderImpl mailSender;
    static{
    //    username = ProjectApplication.getContext().getProjectConfig().getSendemail();
        //password = ProjectApplication.getContext().getProjectConfig().getMailpassword();
        mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.163.com");
        mailSender.setUsername("oracle8090@163.com");
        mailSender.setPassword("123456");
        Properties pro = new Properties();
        pro.setProperty("mail.smtp.auth", "true");
        mailSender.setJavaMailProperties(pro);
    }
    public MailUtils() {
    }
    private static final MailUtils mu = new MailUtils();
   
    public static MailUtils getMailUtils(){
        return mu;
    }
   
    public void sendSimpleEmail(String to, String title, String message) {
    /*    mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.163.com");
        mailSender.setUsername("princejwg.happy@163.com");
        mailSender.setPassword("");
        Properties pro = new Properties();
        pro.setProperty("mail.smtp.auth", "true");
        mailSender.setJavaMailProperties(pro);*/

        if(username==null || password == null){
            //username = ProjectApplication.getContext().getProjectConfig().getSendemail();
            //password = ProjectApplication.getContext().getProjectConfig().getMailpassword();
            mailSender.setUsername("xxxxxxx@163.com");
            mailSender.setPassword("123456");
        }
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(username);
        msg.setTo(to);
        msg.setSubject(title);
        msg.setText(message);
        mailSender.send(msg);
    }

    /**
     * 支持HTML脚本的格式发送Email
     *
     * @param to
     *            发送给谁,对应的email
     * @param title
     *            邮件主题
     * @param message
     *            邮件内容 支持HTML脚本
     * @throws Exception
     */
    public void sendHtmlEmail(String to, String title, String message) {
        JavaMailSenderImpl    mailSender1=mailSender;
        if(username==null || password == null){
            //username = ProjectApplication.getContext().getProjectConfig().getSendemail();
            //password = ProjectApplication.getContext().getProjectConfig().getMailpassword();
            mailSender.setUsername("oracle8090@163.com");
            mailSender.setPassword("123456");
        }
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(msg, true, "GBK");
            helper.setFrom(mailSender.getUsername());
            helper.setTo(to);
            helper.setSubject(title);
            String body = "<html><head><META http-equiv=Content-Type content='text/html; charset=GBK'></HEAD><title>test</title></head><body> ";
            String foot = "<div style='width:300px;foalt:right;'>你司在惺惺惜惺惺想寻寻。</div>";
            foot+= "<div style='width:300px;foalt:right;'>审批通过惺惺惜惺惺想寻寻寻寻进行登录</div>";
            foot+="</body></html>";
            String html = body+message+foot;
            helper.setText(html, true);
            mailSender.send(msg);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
   
    public ProjectContext getPct() {
        return pct;
    }

    public void setPct(ProjectContext pct) {
        this.pct = pct;
    }

    public String getUsername() {
        return username;
    }


    public String getPassword() {
        return password;
    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值