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

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

// 从EML文件得到MimeMessage对象

MimeMessage message = new MimeMessage(session, new FileInputStream(emlFile));

public static String getMailSubject(Message message) throws Exception {

return MimeUtilitydecodeText(messagegetSubject());

}

public static String getMailSender(Message message) throws Exception {

String emailSender = null;

Address[] addresses = messagegetFrom();

if (addresses == null || addresseslength < 1) {

throw new IllegalArgumentException("该邮件没有发件人");

}

// 获得发件人

InternetAddress address = (InternetAddress) addresses[0];

String senderName = addressgetPersonal();

if (senderName != null) {

senderName = MimeUtilitydecodeText(senderName);

emailSender = senderName + "<" + addressgetAddress() + ">";

} else {

senderName = addressgetAddress();

}

return emailSender;

}

public static String getMailRecipients(Message message, MessageRecipientType recipientType) throws Exception {

StringBuilder builder = new StringBuilder();

Address[] addresses = null;

if (recipientType == null) {

addresses = messagegetAllRecipients();

} else {

addresses = messagegetRecipients(recipientType);

}

if (addresses == null || addresseslength < 1) {

throw new IllegalArgumentException("该邮件没有收件人");

}

for (Address address : addresses) {

InternetAddress iAddress = (InternetAddress) address;

builderappend(iAddresstoUnicodeString())append(", ");

}

return builderdeleteCharAt(builderlength() - 1)toString();

}

public static String getMailSendDate(Message message, String pattern) throws Exception {

String sendDateString = null;

if (pattern == null || ""equals(patterntrim())) {

pattern = "yyyy年MM月dd日 E HH:mm";

}

Date sendDate = messagegetSentDate();

sendDateString = new SimpleDateFormat(pattern)format(sendDate);

return sendDateString;

}

public static boolean containsAttachment(Part part) throws Exception {

boolean flag = false;

if (part != null) {

if (partisMimeType("multipart/")) {

MimeMultipart mp = (MimeMultipart) partgetContent();

for (int i = 0; i < mpgetCount(); i++) {

BodyPart bodyPart = mpgetBodyPart(i);

String disposition = bodyPartgetDisposition();

if (disposition != null && (PartATTACHMENTequalsIgnoreCase(disposition)

|| PartINLINEequalsIgnoreCase(disposition))) {

flag = true;

} else if (bodyPartisMimeType("multipart/")) {

flag = containsAttachment(bodyPart);

} else {

String contentType = bodyPartgetContentType();

if (contentTypeindexOf("application") != -1) {

flag = true;

}

if (contentTypeindexOf("name") != -1) {

flag = true;

}

}

if (flag)

break;

}

} else if (partisMimeType("message/rfc822")) {

flag = containsAttachment((Part) partgetContent());

}

}

return flag;

}

public static boolean isSeen(Message message) throws Exception {

if (message == null) {

throw new MessagingException("Message is empty");

}

return messagegetFlags()contains(FlagsFlagSEEN);

}

public static boolean isReplaySign(Message message) throws Exception {

if (message == null) {

throw new MessagingException("Message is empty");

}

boolean replaySign = false;

String[] headers = messagegetHeader("Disposition-Notification-To");

if (headers != null && headerslength > 0) {

replaySign = true;

}

return replaySign;

}

public static String getMailPriority(Message message) throws Exception {

if (message == null) {

throw new MessagingException("Message is empty");

}

String priority = "普通";

String[] headers = messagegetHeader("X-Priority");

if (headers != null && headerslength > 0) {

String mailPriority = headers[0];

if (mailPriorityindexOf("1") != -1 || mailPriorityindexOf("High") != -1) {

priority = "紧急";

} else if (mailPriorityindexOf("5") != -1 || mailPriorityindexOf("Low") != -1) {

priority = "低";

} else {

priority = "普通"; // 3或者Normal;

}

}

return priority;

}

public static void getMailTextContent(Part part, StringBuilder content) throws Exception {

if (part == null) {

throw new MessagingException("Message content is empty");

}

boolean containsTextInAttachment = partgetContentType()indexOf("name") > 0;

if (partisMimeType("text/") && containsTextInAttachment) {

contentappend(partgetContent()toString());

} else if (partisMimeType("message/rfc822")) {

getMailTextContent((Part) partgetContent(), content);

} else if (partisMimeType("multipart/")) {

Multipart mp = (Multipart) partgetContent();

for (int i = 0; i < mpgetCount(); i++) {

BodyPart bodyPart = mpgetBodyPart(i);

getMailTextContent(bodyPart, content);

}

} else if (partisMimeType("image/")) {

// TODO partgetInputStream()获得输入流然后输出到指定的目录

} else {

// TODO 其它类型的contentType, 未做处理, 直接输出

contentappend(partgetContent()toString());

}

}

public static void saveAttachment(Part part, String destDir) throws Exception {

if (part == null) {

throw new MessagingException("part is empty");

}

// 复杂的邮件包含多个邮件体

if (partisMimeType("multipart/")) {

Multipart mp = (Multipart) partgetContent();

// 遍历每一个邮件体

for (int i = 0; i < mpgetCount(); i++) {

BodyPart bodyPart = mpgetBodyPart(i);

// bodyPart也可能有多个邮件体组成

String disposition = bodyPartgetDisposition();

if (disposition == null && (PartATTACHMENTequalsIgnoreCase(disposition)

|| PartINLINEequalsIgnoreCase(disposition))) {

InputStream in = bodyPartgetInputStream();

saveFile(in, destDir, decodeText(bodyPartgetFileName()));

} else if (bodyPartisMimeType("multipart/")) {

saveAttachment(bodyPart, destDir);

} else {

String contentType = bodyPartgetContentType();

if (contentTypeindexOf("name") != -1 || contentTypeindexOf("application") != -1) {

saveFile(bodyPartgetInputStream(), destDir, decodeText(bodyPartgetFileName()));

}

}

}

} else if (partisMimeType("message/rfc822")) {

saveAttachment((Part) partgetContent(), destDir);

}

}

public static void saveFile(InputStream in, String destDir, String fileName) throws Exception {

FileOutputStream out = new FileOutputStream(new File(destDir + fileName));

byte[] buffer = new byte[1024];

int length = 0;

while ((length = inread(buffer)) != -1) {

outwrite(buffer, 0, length);

}

outclose();

inclose();

}

public static String decodeText(String encodedText) throws Exception {

if (encodedText == null || ""equals(encodedTexttrim())) {

return "";

} else {

return MimeUtilitydecodeText(encodedText);

}

}

以上就是关于如何写一个JAVA类可以实现邮件发送功能,也可以实现群发功能全部的内容,包括:如何写一个JAVA类可以实现邮件发送功能,也可以实现群发功能、用java下载一个excel文件,在Response.setContentType()参数中、如何用JAVA实现加载一个文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9570568.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存