`
zhangfeilo
  • 浏览: 391513 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

用 apache commons email 发送带附件,HTML 格式的 邮件

    博客分类:
  • java
阅读更多

1.发送普通纯文本邮件:

SimpleEmail email = new SimpleEmail();
email.setHostName("SMTP服务器");

email.setAuthentication("用户名","密码");

email.addTo("收件人", "收件人名字");
email.setFrom("发件人邮件", "发件人名字");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();

2。发送带附件的邮件

EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D:\\123.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John");

// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("SMTP服务器");

email.setAuthentication("用户名","密码");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");

// add the attachment
email.attach(attachment);

// send the email
email.send();

3.HTML格式邮件

HtmlEmail email = new HtmlEmail();
email.setHostName("SMTP服务器");
email.setAuthentication("用户名","密码");
email.addTo("收件人", "收件人名字");
email.setFrom("发件人邮件", "发件人名字");
  email.setSubject("The picture");
  URL url = new URL("http://****.gif");
  String cid = email.embed(url, "Apache logo");

//   set the html message
  email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

//   set the alternative message
  email.setTextMsg("Your email client does not support HTML messages");

//   send the email
  email.send();
 ===========================================================================

package com.cn.cosoft.util.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;



import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

/**
 * <P>
 * Title:用java发送邮件的例子
 * </P>
 *
 * <P>
 * Description:发送图片附件并在html中使用该图片
 * </P>
 *
 * <P>
 * Copyright: Copyright (c) 2007
 * </P>
 *
 * @author 孙钰佳
 * @main sunyujia@yahoo.cn
 * @date Jun 10, 2008 12:35:26 AM
 */
public class SendMail extends Thread {

    private String username = "a123456";//发送者邮箱用户名
    private String password = "123456";//发送者邮箱密码
    private String smtpServer = "smtp.gmail.com";//发送者邮箱的smtp
    private String fromMailAddress = "a123456@gmail.com";// "yncosoft@gmail.com";//发送者的邮箱
    private int smtpPort = 465;//默认smtp端口号是25
    private String toMailAddress;//接收者的邮箱地在
    private String subject;//主题
    private String message;//邮件正文
    private String filename;//读取某个文件的内容为邮件正文
    public static String getPasswordFile = "getPassword";//获取密码的文件
    public static String registerStatusFile = "registerStatus";//注册验证的邮件
    private String getPasswordStatusCode = "";
    private String registerStatusStatusCode = "";

    public void setGetPasswordStatusCode(String getPasswordStatusCode) {
        this.getPasswordStatusCode = getPasswordStatusCode;
    }

    public void setRegisterStatusStatusCode(String registerStatusStatusCode) {
        this.registerStatusStatusCode = registerStatusStatusCode;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setToMailAddress(String toMailAddress) {
        this.toMailAddress = toMailAddress;
    }

    public static void main(String[] args) throws Exception {
        SendMail send = new SendMail();
        send.setFilename(SendMail.registerStatusFile);
        send.setToMailAddress("123456@qq.com");
        send.setSubject("主题");
        send.sendFileContext();
    }

    @Override
    public void run() {
        try {
            try {
                sendFileContext();
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (EmailException ex) {
            Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendFileContext() throws EmailException, UnsupportedEncodingException {
        HtmlEmail email = new HtmlEmail();
        email.setHostName(smtpServer);//设置SMTP 
        email.setSmtpPort(smtpPort);//设置SMTP 端口
        email.setSSL(true);//设置SMTP 安全:SSL
        email.setAuthentication(username, password);//设置邮件的登录用户名和密码
        email.setFrom(fromMailAddress, "发送者名字");   //设置发送者和名字
        email.addTo(toMailAddress, "接收者密码");//设置接收者和名字
        email.setSubject(subject);//设置邮件主题
//        URL url1 = new URL("http://css.tudouui.com/skin/login/img/logo_0.png");
//        String cid2 = email.embed(url1, "Apache logo2");//附件(附件内容,名字)
        File file = new File(SendMail.class.getResource(filename).toString().substring(5));
        BufferedReader reader = null;
        StringBuilder msg = new StringBuilder("");
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;

            while ((tempString = reader.readLine()) != null) {
                msg.append(tempString);
            }
        } catch (Exception e) {
        }
        message = msg.toString();
        message = new String(message.getBytes("utf-8"), "ISO-8859-1");//处理中文乱码转码
        email.setHtmlMsg(message);
        email.setTextMsg(message);
        email.send();
    }

    public void send() throws EmailException, MalformedURLException, UnsupportedEncodingException {
        HtmlEmail email = new HtmlEmail();
        email.setHostName(smtpServer);
        email.setSmtpPort(smtpPort);
        email.setAuthentication(username, password);
        email.addTo(toMailAddress, toMailAddress);
        email.setFrom(fromMailAddress, fromMailAddress);
        email.setSubject(subject);
//        URL url1 = new URL("http://css.tudouui.com/skin/login/img/logo_0.png");
//        String cid2 = email.embed(url1, "Apache logo2");//附件(附件内容,名字)
        message = new String(message.getBytes("gbk"), "ISO-8859-1");//处理中文乱码转码
        email.setHtmlMsg(message);
        email.setTextMsg("Your email client does not support HTML messages");
        email.send();
    }
}

 

 

分享到:
评论

相关推荐

    commons-email发送邮件

    用 apache commons email 发送带附件,HTML 格式的 邮件 格式例子 还包括commons-email的jar包,及其他相关资料

    Java-ApacheMail发送邮件

    网站中经常用到的功能就是简单的发送一些邮件,比如发送 找回密码信息、报警信息,所以使用Apache Commons Email 写了一个可以复用的类,功能就是简单的发送一些邮件,可以含有收信人、抄送人、按送人、邮件主题、...

    Java编写的邮件发送源文件

    使用Apache的SimpleEmail、HtmlEmail文件,实现了发送简单邮件,Html邮件和发送附件的功能

    Java Email 在Tomcat所依赖的Jar包(全部)

    下面是附件的使用说明.欢迎大家交流. 将文件夹中5个jar包放入lib下即可. 在实际开发中,需要避免不同版本的jar包冲突. 下面是我的开发经验(针对当前具体环境而言): (1) // SimpleMail email = new SimpleMail();...

    自定义 SpringBoot-Starter email-spring-boot-starter.zip

    自己写的starter;使用的apache-commons-email 包内包含源码和已打包的jar,亲测可用,可以直接给126,qq邮箱等直接发邮件,可以包含附件.

    开源bbs源码java-SSHE:社会工程学会

    当前示例演示了包括:权限控制、超大附件文件上传、EasyUI基本组件使用等等功能,具体请自行看本示例演示功能 SSHE框架环境需求:JAVA环境:JDK7+;数据库环境:oracle10g+/sqlserver2000+/mysql5+;WEB容器环境:...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    实例244 发送带附件的Email 308 实例245 读取XML文件属性 310 第3篇 窗体与控件应用 第11章 窗体设计 314 11.1 设置窗体位置 315 实例246 控制窗体加载时的位置 315 实例247 设置窗体在屏幕中的位置 315 实例248 从...

Global site tag (gtag.js) - Google Analytics