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

java发邮件  时间:2021-09-04  阅读:()

如何利用java发送163邮件,最好不要javamail

import?java.util.Properties; import?javax.activation.DataHandler; import?javax.activation.FileDataSource; import?javax.mail.Address; import?javax.mail.BodyPart; import?javax.mail.Message; import?javax.mail.Multipart; import?javax.mail.Session; import?javax.mail.Transport; import?Address; import?.MimeBodyPart; import?.MimeMessage; import?.MimeMultipart; public?class?Mail?{ private?MimeMessage?mimeMsg;?//?MIME邮件对象 private?Session?session;?//?邮件会话对象 private?Properties?props;?//?系统属性 //?smtp认证用户名和密码 private?String?username; private?String?password; private?Multipart?mp;?//?Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 /** ?*?Constructor ?*? ?*?@param?smtp ?*????????????邮件发送服务器 ?*/ public?Mail(String?smtp)?{ setSmtpHost(smtp); createMimeMessage(); } /** ?*?设置邮件发送服务器 ?*? ?*?@param?hostName ?*????????????String ?*/ public?void?setSmtpHost(String?hostName)?{ if?(props?==?null) props?=?System.getProperties();?//?获得系统属性对象 props.put("mail.smtp.host",?hostName);?//?设置SMTP主机 } /** ?*?创建MIME邮件对象 ?*? ?*?@return ?*/ public?boolean?createMimeMessage()?{ try?{ session?=?Session.getDefaultInstance(props,?null);?//?获得邮件会话对象 }?catch?(Exception?e)?{ System.err.println("获取邮件会话对象时发生错误!"?+?e); return?false; } try?{ mimeMsg?=?new?MimeMessage(session);?//?创建MIME邮件对象 mp?=?new?MimeMultipart(); return?true; }?catch?(Exception?e)?{ System.err.println("创建MIME邮件对象失败!"?+?e); return?false; } } /** ?*?设置SMTP是否需要验证 ?*? ?*?@param?need ?*/ public?void?setNeedAuth(boolean?need)?{ if?(props?==?null) props?=?System.getProperties(); if?(need)?{ props.put("mail.smtp.auth",?"true"); }?else?{ props.put("mail.smtp.auth",?"false"); } } /** ?*?设置用户名和密码 ?*? ?*?@param?name ?*?@param?pass ?*/ public?void?setNamePass(String?name,?String?pass)?{ username?=?name; password?=?pass; } /** ?*?设置邮件主题 ?*? ?*?@param?mailSubject ?*?@return ?*/ public?boolean?setSubject(String?mailSubject)?{ try?{ mimeMsg.setSubject(mailSubject); return?true; }?catch?(Exception?e)?{ System.err.println("设置邮件主题发生错误!"); return?false; } } /** ?*?设置邮件正文 ?*? ?*?@param?mailBody ?*????????????String ?*/ public?boolean?setBody(String?mailBody)?{ try?{ BodyPart?bp?=?new?MimeBodyPart(); bp.setContent(""?+?mailBody,?"text/html;charset=utf-8"); mp.addBodyPart(bp); return?true; }?catch?(Exception?e)?{ System.err.println("设置邮件正文时发生错误!"?+?e); return?false; } } /** ?*?添加附件 ?*? ?*?@param?filename ?*????????????String ?*/ public?boolean?addFileAffix(String?filename)?{ try?{ BodyPart?bp?=?new?MimeBodyPart(); FileDataSource?fileds?=?new?FileDataSource(filename); bp.setDataHandler(new?DataHandler(fileds)); bp.setFileName(fileds.getName()); mp.addBodyPart(bp); return?true; }?catch?(Exception?e)?{ System.err.println("增加邮件附件:"?+?filename?+?"发生错误!"?+?e); return?false; } } /** ?*?设置发信人 ?*? ?*?@param?from ?*????????????String ?*/ public?boolean?setFrom(String?from)?{ try?{ mimeMsg.setFrom(new?Address(from));?//?设置发信人 return?true; }?catch?(Exception?e)?{ return?false; } } /** ?*?设置收信人 ?*? ?*?@param?to ?*????????????String ?*/ public?boolean?setTo(String?to)?{ if?(to?==?null) return?false; try?{ mimeMsg.setRecipients(Message.RecipientType.TO, Address.parse(to)); return?true; }?catch?(Exception?e)?{ return?false; } } /** ?*?设置抄送人 ?*? ?*?@param?copyto ?*????????????String ?*/ public?boolean?setCopyTo(String?copyto)?{ if?(copyto?==?null) return?false; try?{ mimeMsg.setRecipients(Message.RecipientType.CC, (Address[])?Address.parse(copyto)); return?true; }?catch?(Exception?e)?{ return?false; } } /** ?*?发送邮件 ?*/ public?boolean?sendOut()?{ try?{ mimeMsg.setContent(mp); mimeMsg.saveChanges(); System.out.println("正在发送邮件...."); Session?mailSession?=?Session.getInstance(props,?null); Transport?transport?=?mailSession.getTransport("smtp"); transport.connect((String)?props.get("mail.smtp.host"),?username, password); transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO)); // 如果抄送人为null??不添加抄送人 if(mimeMsg.getRecipients(Message.RecipientType.CC)?!=?null) transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC)); //?transport.send(mimeMsg); System.out.println("发送邮件成功!"); transport.close(); return?true; }?catch?(Exception?e)?{ System.err.println("邮件发送失败!"?+?e); e.printStackTrace(); return?false; } } /** ?*?调用sendOut方法完成邮件发送 ?*? ?*?@param?smtp ?*?@param?from ?*?@param?to ?*?@param?subject ?*?@param?content ?*?@param?username ?*?@param?password ?*?@return?boolean ?*/ public?static?boolean?send(String?smtp,?String?from,?String?to, String?subject,?String?content,?String?username,?String?password)?{ Mail?theMail?=?new?Mail(smtp); theMail.setNeedAuth(true);?//?需要验证 if?(!theMail.setSubject(subject)) return?false; if?(!theMail.setBody(content)) return?false; if?(!theMail.setTo(to)) return?false; if?(!theMail.setFrom(from)) return?false; theMail.setNamePass(username,?password); if?(!theMail.sendOut()) return?false; return?true; } /** ?*?调用sendOut方法完成邮件发送,带抄送 ?*? ?*?@param?smtp ?*?@param?from ?*?@param?to ?*?@param?copyto ?*?@param?subject ?*?@param?content ?*?@param?username ?*?@param?password ?*?@return?boolean ?*/ public?static?boolean?sendAndCc(String?smtp,?String?from,?String?to, String?copyto,?String?subject,?String?content,?String?username, String?password)?{ Mail?theMail?=?new?Mail(smtp); theMail.setNeedAuth(true);?//?需要验证 if?(!theMail.setSubject(subject)) return?false; if?(!theMail.setBody(content)) return?false; if?(!theMail.setTo(to)) return?false; if?(!theMail.setCopyTo(copyto)) return?false; if?(!theMail.setFrom(from)) return?false; theMail.setNamePass(username,?password); if?(!theMail.sendOut()) return?false; return?true; } /** ?*?调用sendOut方法完成邮件发送,带附件 ?*? ?*?@param?smtp ?*?@param?from ?*?@param?to ?*?@param?subject ?*?@param?content ?*?@param?username ?*?@param?password ?*?@param?filename ?*????????????附件路径 ?*?@return ?*/ public?static?boolean?send(String?smtp,?String?from,?String?to, String?subject,?String?content,?String?username,?String?password, String?filename)?{ Mail?theMail?=?new?Mail(smtp); theMail.setNeedAuth(true);?//?需要验证 if?(!theMail.setSubject(subject)) return?false; if?(!theMail.setBody(content)) return?false; if?(!theMail.addFileAffix(filename)) return?false; if?(!theMail.setTo(to)) return?false; if?(!theMail.setFrom(from)) return?false; theMail.setNamePass(username,?password); if?(!theMail.sendOut()) return?false; return?true; } /** ?*?调用sendOut方法完成邮件发送,带附件和抄送 ?*? ?*?@param?smtp ?*?@param?from ?*?@param?to ?*?@param?copyto ?*?@param?subject ?*?@param?content ?*?@param?username ?*?@param?password ?*?@param?filename ?*?@return ?*/ public?static?boolean?sendAndCc(String?smtp,?String?from,?String?to, String?copyto,?String?subject,?String?content,?String?username, String?password,?String?filename)?{ Mail?theMail?=?new?Mail(smtp); theMail.setNeedAuth(true);?//?需要验证 if?(!theMail.setSubject(subject)) return?false; if?(!theMail.setBody(content)) return?false; if?(!theMail.addFileAffix(filename)) return?false; if?(!theMail.setTo(to)) return?false; if?(!theMail.setCopyTo(copyto)) return?false; if?(!theMail.setFrom(from)) return?false; theMail.setNamePass(username,?password); if?(!theMail.sendOut()) return?false; return?true; } public?static?void?main(String[]?args)?{ //// SMTP服务器 String?smtp?=?"xxx"; // 发信人 String?from?=?"xxx"; String?to?=?"xxx"; String?subject?=?"xxx"; String?content?=?"xxx"; String?username?=?"xxx"; String?password?=?"xxx"; Mail.send(smtp,?from,?to,?subject,?content,?username,?password); } }这个是我现在在用的

如何写一个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 .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; /** * 该类使用Socket连接到邮件服务器, 并实现了向指定邮箱发送邮件及附件的功能。

* * @author Kou Hongtao */ public class Email { /** * 换行符 */ private static final String LINE_END = " "; /** * 值为“true”输出高度信息(包括服务器响应信息),值为“ false”则不输出调试信息。

*/ private boolean isDebug = true; /** * 值为“true”则在发送邮件{@link Mail#send()} 过程中会读取服务器端返回的消息, * 并在邮件发送完毕后将这些消息返回给用户。

*/ private boolean isAllowReadSocketInfo = true; /** * 邮件服务器地址 */ private String host; /** * 发件人邮箱地址 */ private String from; /** * 收件人邮箱地址 */ private List to; /** * 抄送地址 */ private List; /** * 暗送地址 */ private List ; /** * 邮件主题 */ private String subject; /** * 用户名 */ private String user; /** * 密码 */ private String password; /** * MIME邮件类型 */ private String contentType; /** * 用来绑定多个邮件单元{@link #partSet} * 的分隔标识,我们可以将邮件的正文及每一个附件都看作是一个邮件单元 。

*/ private String boundary; /** * 邮件单元分隔标识符,该属性将用来在邮件中作为分割各个邮件单元的标识 。

*/ private String boundaryNextPart; /** * 传输邮件所采用的编码 */ private String contentTransferEncoding; /** * 设置邮件正文所用的字符集 */ private String charset; /** * 内容描述 */ private String contentDisposition; /** * 邮件正文 */ private String content; /** * 发送邮件日期的显示格式 */ private String simpleDatePattern; /** * 附件的默认MIME类型 */ private String defaultAttachmentContentType; /** * 邮件单元的集合,用来存放正文单元和所有的附件单元。

*/ private List partSet; private List alternativeList; private String mixedBoundary; private String mixedBoundaryNextPart; /** * 不同类型文件对应的{@link MIME} 类型映射。

在添加附件 * {@link #addAttachment(String)} 时,程序会在这个映射中查找对应文件的 * {@link MIME} 类型,如果没有, 则使用 * {@link #defaultAttachmentContentType} 所定义的类型。

*/ 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"); } /** * 该类用来实例化一个正文单元或附件单元对象,他继承了 {@link Mail} * ,在这里制作这个子类主要是为了区别邮件单元对象和邮件服务对象 ,使程序易读一些。

* 这些邮件单元全部会放到partSet 中,在发送邮件 {@link #send()}时, 程序会调用 * {@link #getAllParts()} 方法将所有的单元合并成一个符合MIME格式的字符串。

* * @author Kou Hongtao */ 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(); = new ArrayList(); = new ArrayList(); mixedBoundary = "=NextAttachment_zlz_" + System.currentTimeMillis(); mixedBoundaryNextPart = "--" + mixedBoundary; } /** * 根据指定的完整文件名在 {@link #contentTypeMap} 中查找其相应的MIME类型, * 如果没找到,则返回 {@link #defaultAttachmentContentType} * 所指定的默认类型。

* * @param fileName * 文件名 * @return 返回文件对应的MIME类型。

*/ 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; } /** * 将给定字符串转换为base64编码的字符串 * * @param str * 需要转码的字符串 * @param charset * 原字符串的编码格式 * @return base64编码格式的字符 */ private String toBase64(String str, String charset) { if (null != str) { try { return toBase64(str.getBytes(charset)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return ""; } /** * 将指定的字节数组转换为base64格式的字符串 * * @param bs * 需要转码的字节数组 * @return base64编码格式的字符 */ private String toBase64(byte[] bs) { return new BASE64Encoder().encode(bs); } /** * 将给定字符串转换为base64编码的字符串 * * @param str * 需要转码的字符串 * @return base64编码格式的字符 */ private String toBase64(String str) { return toBase64(str, Charset.defaultCharset().name()); } /** * 将所有的邮件单元按照标准的MIME格式要求合并。

* * @return 返回一个所有单元合并后的字符串。

*/ 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(); }

妮妮云(100元/月)阿里云香港BGP专线 2核 4G

妮妮云的来历妮妮云是 789 陈总 张总 三方共同投资建立的网站 本着“良心 便宜 稳定”的初衷 为小白用户避免被坑妮妮云的市场定位妮妮云主要代理市场稳定速度的云服务器产品,避免新手购买云服务器的时候众多商家不知道如何选择,妮妮云就帮你选择好了产品,无需承担购买风险,不用担心出现被跑路 被诈骗的情况。妮妮云的售后保证妮妮云退款 通过于合作商的友好协商,云服务器提供2天内全额退款,超过2天不退款 物...

Raksmart:香港高防服务器/20Mbps带宽(cn2+bgp)/40G-100Gbps防御

RAKsmart怎么样?RAKsmart香港机房新增了付费的DDoS高防保护服务,香港服务器默认接入20Mbps的大陆优化带宽(电信走CN2、联通和移动走BGP)。高防服务器需要在下单页面的IP Addresses Option里面选择购买,分:40Gbps大陆优化高防IP-$461/月、100Gbps国际BGP高防IP-$692/月,有兴趣的可以根据自己的需求来选择!点击进入:RAKsmart官...

UCloud:全球大促降价,云服务器全网最低价,1核1G快杰云服务器47元/年

ucloud:全球大促活动降价了!这次云服务器全网最低价,也算是让利用户了,UCloud商家调低了之前的促销活动价格,并且新增了1核1G内存配置快杰型云服务器,价格是47元/年(也可选2元首月),这是全网同配置最便宜的云服务器了!UCloud全球大促活动促销机型有快杰型云服务器和通用型云服务器,促销机房国内海外都有,覆盖全球20个城市,具体有北京、上海、广州、香港、 台北、日本东京、越南胡志明市、...

java发邮件为你推荐
嵌入式开发嵌入式开发是什么模糊数学模糊数学的产生体系文件ISO体系文件分级软件详细设计说明书软件产品规格说明书都包含什么内容nvidia官方网站NVIDIA显卡驱动jsp源码在网上下的jsp源码怎么运行?有数据库的招行信用卡还款我是招行的信用卡!该怎么还款教学视频网站谁有各种教学视频网站呀.?黑屏操作电脑黑屏,什么都操作不了修复网络lspLSP修复是什么意思?
vps 双线虚拟主机 老域名失效请用户记下 Oray域名注册服务商 adman singlehop bluevm 美国主机网 华为云主机 免费ddos防火墙 个人免费空间 北京双线 metalink 新世界服务器 in域名 银盘服务 七夕快乐英语 万网空间管理 net空间 谷歌台湾 更多