永发信息网

如何写一个JAVA类可以实现邮件发送功能,也可以实现群发功能

答案:2  悬赏:0  手机版
解决时间 2021-03-24 05:31
如何写一个JAVA类可以实现邮件发送功能,也可以实现群发功能
最佳答案
package byd.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sun.misc.BASE64Encoder;


public class Email {


private static final String LINE_END = "\r\n";


private boolean isDebug = true;


private boolean isAllowReadSocketInfo = true;


private String host;


private String from;


private List to;


private List cc;


private List bcc;


private String subject;


private String user;


private String password;


private String contentType;


private String boundary;


private String boundaryNextPart;


private String contentTransferEncoding;


private String charset;


private String contentDisposition;


private String content;


private String simpleDatePattern;


private String defaultAttachmentContentType;


private List partSet;

private List alternativeList;

private String mixedBoundary;

private String mixedBoundaryNextPart;


private static Map contentTypeMap;

private static enum TextType {
PLAIN("plain"), HTML("html");

private String v;

private TextType(String v) {
this.v = v;
}

public String getValue() {
return this.v;
}
}

static {
// MIME Media Types
contentTypeMap = new HashMap();
contentTypeMap.put("xls", "application/vnd.ms-excel");
contentTypeMap.put("xlsx", "application/vnd.ms-excel");
contentTypeMap.put("xlsm", "application/vnd.ms-excel");
contentTypeMap.put("xlsb", "application/vnd.ms-excel");
contentTypeMap.put("doc", "application/msword");
contentTypeMap.put("dot", "application/msword");
contentTypeMap.put("docx", "application/msword");
contentTypeMap.put("docm", "application/msword");
contentTypeMap.put("dotm", "application/msword");
}


private class MailPart extends Email {
public MailPart() {
}
}


public Email() {
defaultAttachmentContentType = "application/octet-stream";
simpleDatePattern = "yyyy-MM-dd HH:mm:ss";
boundary = "--=_NextPart_zlz_3907_" + System.currentTimeMillis();
boundaryNextPart = "--" + boundary;
contentTransferEncoding = "base64";
contentType = "multipart/mixed";
charset = Charset.defaultCharset().name();
partSet = new ArrayList();
alternativeList = new ArrayList();
to = new ArrayList();
cc = new ArrayList();
bcc = new ArrayList();
mixedBoundary = "=NextAttachment_zlz_" + System.currentTimeMillis();
mixedBoundaryNextPart = "--" + mixedBoundary;
}


private String getPartContentType(String fileName) {
String ret = null;
if (null != fileName) {
int flag = fileName.lastIndexOf(".");
if (0 <= flag && flag < fileName.length() - 1) {
fileName = fileName.substring(flag + 1);
}
ret = contentTypeMap.get(fileName);
}

if (null == ret) {
ret = defaultAttachmentContentType;
}
return ret;
}


private String toBase64(String str, String charset) {
if (null != str) {
try {
return toBase64(str.getBytes(charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "";
}


private String toBase64(byte[] bs) {
return new BASE64Encoder().encode(bs);
}


private String toBase64(String str) {
return toBase64(str, Charset.defaultCharset().name());
}


private String getAllParts() {

StringBuilder sbd = new StringBuilder(LINE_END);
sbd.append(mixedBoundaryNextPart);
sbd.append(LINE_END);
sbd.append("Content-Type: ");
sbd.append("multipart/alternative");
sbd.append(";");
sbd.append("boundary=\"");
sbd.append(boundary).append("\""); // 邮件类型设置
sbd.append(LINE_END);
sbd.append(LINE_END);
sbd.append(LINE_END);
addPartsToString(alternativeList, sbd, getBoundaryNextPart());
sbd.append(getBoundaryNextPart()).append("--");
sbd.append(LINE_END);

addPartsToString(partSet, sbd, mixedBoundaryNextPart);

sbd.append(LINE_END);
sbd.append(mixedBoundaryNextPart).append("--");
sbd.append(LINE_END);
// sbd.append(boundaryNextPart).
// append(LINE_END);
alternativeList.clear();
partSet.clear();
return sbd.toString();
}
全部回答
现成的东西。需要给你。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
江苏南通86岁胡汉生老人在修理自行车时因心脏
高博特(上南配送店)在什么地方啊,我要过去处
共青至九江动车时刻表
夸男生长得帅的诗词,形容一个男孩长得帅,怎
宝鸡古玩成在哪
想买辆野马t70,不知道这车怎么样?
还有几天就到情人节了?
女35岁心跳109怎么办
怎样干净去除眼影
请以“珍惜与放弃”为话题,写一篇文章
框架结构中有后浇带时最高层连屋顶也分层浇的
魅族MX6 3+32的这款怎么样,和魅蓝note5 4+64
求曲线y=x和直线y=3x所围成的面积。
35加65乘四十除以五用简便的算法来算
朝阳新区六街/环城西路五街(路口)地址有知道
推荐资讯
艺龙上可以用支付宝付款吗?
北京祛颈纹哪家好
摩诃萨怎么读?
单选题下列有关基因和染色体关系的叙述不正确
喜欢图片背影的人都有着怎样的心情
轻松筹提现需要什么,轻松筹怎么发起项目?轻
常州到桐庐多少公里,我从浦江到桐庐的客车表
鞋垫上印wei.si.ai.er是什么牌子呀?
下列斗争迫使近代中国新旧政权更替的包括①辛
centos 7能搭建ssr吗
关于eastbay订单状态的求助
徒弟称赞师傅的话
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?