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

港云网络(¥1/月活动机器),香港CN2 4核4G 1元/月 美国CN2

港云网络官方网站商家简介港云网络成立于2016年,拥有IDC/ISP/云计算资质,是正规的IDC公司,我们采用优质硬件和网络,为客户提供高速、稳定的云计算服务。公司拥有一流的技术团队,提供7*24小时1对1售后服务,让您无后顾之忧。我们目前提供高防空间、云服务器、物理服务器,高防IP等众多产品,为您提供轻松上云、安全防护。点击进入港云网络官方网站港云网络中秋福利1元领【每人限量1台】,售完下架,活...

数脉科技:六月优惠促销,免备案香港物理服务器,E3-1230v2处理器16G内存,350元/月

数脉科技六月优惠促销发布了!数脉科技对香港自营机房的香港服务器进行超低价促销,可选择30M、50M、100Mbps的优质bgp网络。更大带宽可在选购时选择同样享受优惠,目前仅提供HKBGP、阿里云产品,香港CN2、产品优惠码续费有效,仅限新购,每个客户可使用于一个订单。新客户可以立减400元,或者选择对应的机器用相应的优惠码,有需要的朋友可以尝试一下。点击进入:数脉科技官方网站地址数脉科技是一家成...

HostKvm:香港国际/韩国KVM夏季7折,2G内存套餐月付5.95美元起

HostKvm是一家成立于2013年的国外主机服务商,主要提供基于KVM架构的VPS主机,可选数据中心包括日本、新加坡、韩国、美国、中国香港等多个地区机房,均为国内直连或优化线路,延迟较低,适合建站或者远程办公等。目前商家发布了夏季特别促销活动,针对香港国际/韩国机房VPS主机提供7折优惠码,其他机房全场8折,优惠后2GB内存套餐月付5.95美元起。下面分别列出几款主机套餐配置信息。套餐:韩国KR...

java发邮件为你推荐
连接池什么是数据连接池?javaHDvideojava手机视频转换器burndown如何画burndown chart开房数据库ODBC数据库是什么呢?电商网站设计电商网站设计需要注意哪些黑屏操作麻烦问一下 黑屏的各个指令安全网络攻防大赛CTF是什么意思云弟关于石榴的古诗申请信箱怎么申请邮箱啊360后门360真的给用户开后门吗
欧洲欧洲vps 域名备案流程 域名备案只选云聚达 中国域名交易中心 lnmp sharktech dux 微信收钱 七夕快乐英文 me空间社区 美国堪萨斯 免费phpmysql空间 万网空间购买 免费邮件服务器 lamp兄弟连 徐州电信 测试网速命令 广州服务器托管 shuangcheng winserver2008r2 更多