java发邮件Java怎么直接发送邮件,而不通过页面或者outlook软件.

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

怎样用java实现邮件的发送?

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import .Socket; import .SocketException; import java.rmi.UnknownHostException; import java.util.StringTokenizer; import sun.misc.BASE64Encoder; public class Sender { //private boolean debug = true; BASE64Encoder encode=new BASE64Encoder();//用于加密后发送用户名和密码 static int dk=25; private Socket socket; public Sender(String server, int port) throws UnknownHostException, IOException { try { socket = new Socket(server, dk); } catch (SocketException e) { System.out.println(e.getMessage()); } catch (Exception e) { e.printStackTrace(); } finally { //System.out.println("已经建立连接!"); } } // 注册到邮件服务器 public void helo(String server, BufferedReader in, BufferedWriter out) throws IOException { int result; result = getResult(in); // 连接上邮件服务后,服务器给出220应答 if (result != 220) { throw new IOException("连接服务器失败"); } result = sendServer("HELO " + server, in, out); // HELO命令成功后返回250 if (result != 250) { throw new IOException("注册邮件服务器失败!"); } } private int sendServer(String str, BufferedReader in, BufferedWriter out) throws IOException { out.write(str); out.newLine(); out.flush(); /* if (debug) { System.out.println("已发送命令:" + str); } */ return getResult(in); } public int getResult(BufferedReader in) { String line = ""; try { line = in.readLine(); /* if (debug) { System.out.println("服务器返回状态:" + line); } */ } catch (Exception e) { e.printStackTrace(); } // 从服务器返回消息中读出状态码,将其转换成整数返回 StringTokenizer st = new StringTokenizer(line, " "); return Integer.parseInt(st.nextToken()); } public void authLogin(MailMessage message, BufferedReader in, BufferedWriter out) throws IOException { int result; result = sendServer("AUTH LOGIN", in, out); if (result != 334) { throw new IOException("用户验证失败!"); } result=sendServer(encode.encode(message.getUser().getBytes()),in,out); //System.out.println("用户名: "+encode.encode(message.getUser().getBytes())); if (result != 334) { throw new IOException("用户名错误!"); } result=sendServer(encode.encode(message.getPassword().getBytes()),in,out); //result=sendServer(message.getPassword(),in,out); //System.out.println("密码: "+encode.encode(message.getPassword().getBytes())); if (result != 235) { throw new IOException("验证失败!"); } } // 开始发送消息,邮件源地址 public void mailfrom(String source, BufferedReader in, BufferedWriter out) throws IOException { int result; result = sendServer("MAIL FROM:<" + source + ">", in, out); if (result != 250) { throw new IOException("指定源地址错误"); } } // 设置邮件收件人 public void rcpt(String touchman, BufferedReader in, BufferedWriter out) throws IOException { int result; result = sendServer("RCPT TO:<" + touchman + ">", in, out); if (result != 250) { throw new IOException("指定目的地址错误!"); } } // 邮件体 public void data(String from, String to, String subject, String content, BufferedReader in, BufferedWriter out) throws IOException { int result; result = sendServer("DATA", in, out); // 输入DATA回车后,若收到354应答后,继续输入邮件内容 if (result != 354) { throw new IOException("不能发送数据"); } out.write("From: " + from); out.newLine(); out.write("To: " + to); out.newLine(); out.write("Subject: " + subject); out.newLine(); out.newLine(); out.write(content); out.newLine(); // 句号加回车结束邮件内容输入 result = sendServer(".", in, out); //System.out.println(result); if (result != 250) { throw new IOException("发送数据错误"); } } // 退出 public void quit(BufferedReader in, BufferedWriter out) throws IOException { int result; result = sendServer("QUIT", in, out); if (result != 221) { throw new IOException("未能正确退出"); } } // 发送邮件主程序 public boolean sendMail(MailMessage message, String server) { try { BufferedReader in = new BufferedReader(new InputStreamReader( socket.getInputStream())); BufferedWriter out = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())); helo(server, in, out);// HELO命令 authLogin(message, in, out);// AUTH LOGIN命令 mailfrom(message.getFrom(), in, out);// MAIL FROM rcpt(message.getTo(), in, out);// RCPT data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);// DATA quit(in, out);// QUIT } catch (Exception e) { e.printStackTrace(); return false; } return true; } } 再写一个MailMessage.java,set/get方法即可。

怎么用java发送带附件的邮件代码详解

package?email;?? 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;?? public?class?Mail?{?? private?static?final?String?LINE_END?=?" ";?? private?boolean?isDebug?=?true;?? 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;?? 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?static?Map?contentTypeMap;?? 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?Mail?{?? public?MailPart()?{?? }?? }?? public?Mail()?{?? defaultAttachmentContentType?=?"application/octet-stream";?? simpleDatePattern?=?"yyyy-MM-dd?HH:mm:ss";?? boundary?=?"--=_NextPart_zlz_3907_"?+?System.currentTimeMillis();?? boundaryNextPart?=?"--"?+?boundary;?? contentTransferEncoding?=?"base64";?? contentType?=?"multipart/alternative";?? charset?=?Charset.defaultCharset().name();?? partSet?=?new?ArrayList();?? to?=?new?ArrayList();?? cc?=?new?ArrayList();?? ?=?new?ArrayList();?? }?? private?String?getPartContentType(String?fileName)?{?? String?ret?=?null;?? if?(null?!=?fileName)?{?? int?flag?=?fileName.lastIndexOf(".");?? if?(0?<=?flag?&&?flag?=?0;?i--)?{?? Mail?attachment?=?partSet.get(i);?? String?attachmentContent?=?attachment.getContent();?? if?(null?!=?attachmentContent?&&?0??mailAddressList)?{?? StringBuilder?sbd?=?new?StringBuilder();?? if?(null?!=?mailAddressList)?{?? int?listSize?=?mailAddressList.size();?? for?(int?i?=?0;?i?");?? }?? }?? return?sbd.toString();?? }?? private?List?getrecipient()?{?? List?list?=?new?ArrayList();?? list.addAll(to);?? );?? list.addAll();?? return?list;?? }?? public?void?addAttachment(String?filePath)?{?? addAttachment(filePath,?null);?? }?? public?void?addTo(String?mailAddress)?{?? this.to.add(mailAddress);?? }?? public?void?addCc(String?mailAddress)?{?? .add(mailAddress);?? }?? public?void?(String?mailAddress)?{?? .add(mailAddress);?? }?? public?void?addAttachment(String?filePath,?String?charset)?{?? if?(null?!=?filePath?&&?filePath.length()?>?0)?{?? File?file?=?new?File(filePath);?? try?{?? addAttachment(file.getName(),?new?FileInputStream(file),?? charset);?? }?catch?(FileNotFoundException?e)?{?? System.out.println("错误:"?+?e.getMessage());?? System.exit(1);?? }?? }?? }

如何把java程序运行结果发送到邮箱

可以选择使用log4j,它是一款开源的日志记录工具,提供发送日志邮件功能 Log4j发送日志邮件的作用: 项目错误信息能及时(实时)反映给项目维护人员以及相关负责人。

优点: 1.快速响应; 2.共同监督; 3.邮件正文直接显示了错误信息,拷贝信息比登陆服务器再查找要方便; 4.在日志信息继续写入文件的前提下,多了另外一种获取信息的渠道。

补充:Log4j可以实现输出到控制台,文件,回滚文件,发送日志邮件,数据库,自定义标签。

发送邮件的一个重要的类是SMTPAppender,之前用的是 log4j-1.2.8,在1.2.8的版本中,SMTPAppender没有SMTPPassword 和SMTPUsername 属性。

这两个属性分别是登录SMTP服务器发送认证的用户名和密码。

依赖的jar包: log4j-1.2.15.jar(版本低于log4j-1.2.14.jar不支持SMTP认证) mail.jar activation.jar 在log4j.properties文件中配置: ### send error through email. #log4j的邮件发送appender,如果有必要你可以写自己的appender .SMTPAppender #发送邮件的门槛,仅当等于或高于ERROR(比如FATAL)时,邮件才被发送 log4j.appender.MAIL.Threshold=ERROR #缓存文件大小,日志达到10k时发送Email log4j.appender.MAIL.BufferSize=10 #发送邮件的邮箱帐号 log4j.appender.MAIL.From=xxx@ #SMTP邮件发送服务器地址 log4j.appender.MAIL.SMTPHost= #SMTP发送认证的帐号名 log4j.appender.MAIL.SMTPUsername=xxx@ #SMTP发送认证帐号的密码 log4j.appender.MAIL.SMTPPassword=xxx #是否打印调试信息,如果选true,则会输出和SMTP之间的握手等详细信息 log4j.appender.MAIL.SMTPDebug=false #邮件主题 log4j.appender.MAIL.Subject=Log4JErrorMessage #发送到什么邮箱,如果要发送给多个邮箱,则用逗号分隔; #如果需要发副本给某人,则加入下列行 #=xxx@xxx.xxx log4j.appender.MAIL.To=xxx@ .apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=[framework]%d - %c -%-4r[%t]%-5p %c %x -%m%n 在java代码中,可是用logger.info("message");方法将message代表的消息发送到指定邮箱中

Java怎么直接发送邮件,而不通过页面或者outlook软件.

1 必须编写邮件客户端程序,请使用javamail包 2 编写一个页面,触发一个事件,讲发送的内容传递给后台的邮件客户端程序,即可完成你的要求 3 你不会是要求我们替你写个程序吧??? 祝你好运!

CloudCone,美国洛杉矶独立服务器特价优惠,美国洛杉矶MC机房,100Mbps带宽不限流量,可选G口,E3-1270 v2处理器32G内存1Gbps带宽,69美元/月

今天CloudCone发布了最新的消息,推送了几款特价独立服务器/杜甫产品,美国洛杉矶MC机房,分配100Mbps带宽不限流量,可以选择G口限制流量计划方案,存储分配的比较大,选择HDD硬盘的话2TB起,MC机房到大陆地区线路还不错,有需要美国特价独立服务器的朋友可以关注一下。CloudCone怎么样?CloudCone服务器好不好?CloudCone值不值得购买?CloudCone是一家成立于2...

鲸云10美元,香港BGPRM 1核 1G 10Mbps峰值带宽 1TB流量,江西CN2-NAT 1核 512MB内存 100M带宽 ,

WHloud Official Notice(鲸云官方通知)(鲸落 梦之终章)]WHloud RouMu Cloud Hosting若木产品线云主机-香港节点上新预售本次线路均为电信CN2 GIA+移动联通BGP,此机型为正常常规机,建站推荐。本次预售定为国庆后开通,据销售状况决定,照以往经验或有咕咕的可能性,但是大多等待时间不长。均赠送2个快照 2个备份,1个默认ipv4官方网站:https:/...

bgpto:独立服务器夏季促销,日本机器6.5折、新加坡7.5折,20M带宽,低至$93/月

bgp.to对日本机房、新加坡机房的独立服务器在搞特价促销,日本独立服务器低至6.5折优惠,新加坡独立服务器低至7.5折优惠,所有优惠都是循环的,终身不涨价。服务器不限制流量,支持升级带宽,免费支持Linux和Windows server中文版(还包括Windows 10). 特色:自动部署,无需人工干预,用户可以在后台自己重装系统、重启、关机等操作!官方网站:https://www.bgp.to...

java发邮件为你推荐
fast路由器如何设置fast无线路由器具体步骤搜索引擎有哪些1.什么是搜索引擎?举出几个常用的搜索引擎。nvidia官方网站怎么下载英伟达显卡驱动官方jsp源码jsp 中网站的首页源代码4g上网卡联通4g上网卡是什么?网络图绘制双代号网络图的绘制方法?0x800ccc0f任务“POP3.163.COM-正在接收”报告了错误(0x800CCC0F)互动电视什么是“华数互动电视”?soap是什么意思捡肥皂是什么意思啊?无恶意,就好奇360官网打不开360官网进不了怎么办
服务器日志分析 lamp配置 牛人与腾讯客服对话 html空间 ftp教程 国外代理服务器地址 东莞服务器 多线空间 优酷黄金会员账号共享 yundun lick web应用服务器 主机返佣 中国联通宽带测速 中国联通宽带测试 攻击服务器 宿迁服务器 阿里云邮箱个人版 湖南铁通 优惠服务器 更多