设为首页 收藏本站
查看: 631|回复: 0

[经验分享] 用 apache commons email 发送带附件,HTML 格式的 邮件

[复制链接]

尚未签到

发表于 2017-1-13 11:17:44 | 显示全部楼层 |阅读模式
  
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();
    }
}

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-327943-1-1.html 上篇帖子: fastcgi实现apache+php 以及安装php加速器ZendOptimizer-3.3.9 下篇帖子: apache自带负载均衡的集群功能实战录(mod_proxy模块的应用)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表