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

搬瓦工(季付46.7美元)新增荷兰(联通线路)VPS,2.5-10Gbps

搬瓦工最近新增了荷兰机房中国联通(AS9929、AS4837)线路的VPS产品,选项为NL - China Unicom Amsterdam(ENUL_9),与日本软银和洛杉矶(DC06)CN2 GIA配置一致可以互换,属于高端系列,2.5Gbps-10Gbps大带宽,最低季付46.7美元起。搬瓦工VPS隶属于老牌IT7公司旗下,主要提供基于KVM架构VPS主机,数据中心包括美国洛杉矶、凤凰城、纽...

搬瓦工:香港PCCW机房即将关闭;可免费升级至香港CN2 GIA;2核2G/1Gbps大带宽高端线路,89美元/年

搬瓦工怎么样?这几天收到搬瓦工发来的邮件,告知香港pccw机房(HKHK_1)即将关闭,这也不算是什么出乎意料的事情,反而他不关闭我倒觉得奇怪。因为目前搬瓦工香港cn2 GIA 机房和香港pccw机房价格、配置都一样,可以互相迁移,但是不管是速度还是延迟还是丢包率,搬瓦工香港PCCW机房都比不上香港cn2 gia 机房,所以不知道香港 PCCW 机房存在还有什么意义?关闭也是理所当然的事情。点击进...

legionbox:美国、德国和瑞士独立服务器,E5/16GB/1Gbps月流量10TB起/$69/月起

legionbox怎么样?legionbox是一家来自于澳大利亚的主机销售商,成立时间在2014年,属于比较老牌商家。主要提供VPS和独立服务器产品,数据中心包括美国洛杉矶、瑞士、德国和俄罗斯。其中VPS采用KVM和Xen架构虚拟技术,硬盘分机械硬盘和固态硬盘,系统支持Windows。当前商家有几款大硬盘的独立服务器,可选美国、德国和瑞士机房,有兴趣的可以看一下,付款方式有PAYPAL、BTC等。...

java发邮件为你推荐
网络技术与应用网络技术与软件的技术的区别是什么java队列java 队列eofexceptionjava出现异常Exception in thread "main" java.io.EOFExceptiona8处理器AMD A8处理器与I5比怎么样监控员工公司如何监控员工手机和微信工作经验介绍个人简历中工作经验怎么写?网络购物的发展网络购物的发展对策科学计算器说明书计算器的使用方法?约束是什么意思约束,是什么意思。如有回答,请详细,网游木马最新网游木马及其防范技巧
合租服务器 谷歌域名邮箱 hawkhost jsp主机 免费cdn加速 监控宝 ubuntu更新源 创梦 web服务器架设 双拼域名 网站木马检测工具 免费吧 亚马逊香港官网 美国堪萨斯 万网主机管理 德隆中文网 万网注册 免费网络空间 宿迁服务器 卡巴斯基试用版下载 更多