写了一个收发邮件的应用程序[在列表里面可以看到]但是毕竟有些复杂,关键部分其实也就是几行代码,为了大家使用方便,我把发送邮件的代码单独拿了出来,并且分为发送附件/不发送附件两个方法,便于大家查看,只是什么设计啦,编程思想啦,等等就谈不到了,呵呵,大家将就吧

JDK版本
        1.4.x
    其   他
        JAVAMAIL相关包
    功能简介:
        简单的邮件发送功能,可以发送附件

源代码如下

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import java.util.*;
import java.text.*;
import java.io.*;
public class SendMail
{
//传入的参数有密码、姓名、谁发、发给谁、主题、正文内容、smtp地址、附件文件路径、附件的新文件名、发送类型(text/html)
//发送邮件主函数
public String sendmail(int myport,String password,String username,String myfrom,String myto,String mysubject,String mytext,String mysmtp,String[] filepath,String[] newfilename,String htmlandtext)
{
try{
int indexstr=0;
if (filepath[0]!=null && !filepath[0].equals(""))
indexstr=1;
//替换字符串
//     jbemail myjbemail=new jbemail();
//     filepath=myjbemail.myreplace(filepath,"\\","\\\\");
//     System.out.println("附件地址"+filepath+"服务器地址"+mysmtp+mysmtp.length());
//Properties props = new Properties();
Properties props = System.getProperties();
Session sendMailSession;
Store store;  //收邮件时使用
Transport transport;//发邮件时使用
props.put("mail.smtp.host",mysmtp);
props.put("mail.smtp.auth","true");
SmtpAuthenticator sa=new SmtpAuthenticator(username,password);
sendMailSession = Session.getInstance(props,sa);
//sendMailSession = Session.getInstance(props,null);
sendMailSession.setDebug(true);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(myfrom));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(myto));
//设定邮件格式
newMessage.setSentDate(new Date());
System.out.println(htmlandtext+"邮件正文格式");
Multipart multipart = new MimeMultipart();
if (htmlandtext.equals("text"))
{
//获得文本格式的邮件
newMessage.setSubject(mysubject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(mytext);
multipart.addBodyPart(messageBodyPart);
}
else if(htmlandtext.equals("html"))
{
//设置邮件内容,将邮件body部分转化为HTML格式
newMessage.setSubject(mysubject,"gb2312");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mytext,"text/html;charset=gb2312"));
multipart.addBodyPart(messageBodyPart);
}
if (indexstr>0)
{
for(int i=0;i                    {
if (newfilename[i]!=null)
{
//创建BodyPart对象以便获得附件
BodyPart messageBodyPart = new MimeBodyPart();
System.out.println("附件地址"+filepath[i]);
DataSource source = new FileDataSource(filepath[i]);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(newfilename[i]);
multipart.addBodyPart(messageBodyPart);
}
}
}
//将正文和附件添加到邮件中
newMessage.setContent(multipart);
newMessage.saveChanges();
//transport = sendMailSession.getStore("pop3");
transport = sendMailSession.getTransport("smtp");
transport.connect(mysmtp,myport,username,password);
//transport.connect();
transport.send(newMessage,newMessage.getAllRecipients());
System.out.println("成功发送到"+myto);
return "ok";
}
catch(MessagingException m)
{
System.out.println(m.toString()+"失败");
return myto;
}
}
//不含发送附件的函数
//传入的参数有port地址、密码、姓名、谁发、发给谁、主题、正文内容、smtp地址、发送类型(text/html)
public String sendmail(String mailPathlog,int myport,String password,String username,String myfrom,String myto,String mysubject,String mytext,String mysmtp,String htmlandtext)
{
try{
//解码
mysubject=java.net.URLDecoder.decode(mysubject);
//Properties props = new Properties();
Properties props = System.getProperties();
Session sendMailSession;
Store store;  //收邮件时使用
Transport transport;//发邮件时使用
props.put("mail.smtp.host",mysmtp);
props.put("mail.smtp.auth","true");
SmtpAuthenticator sa=new SmtpAuthenticator(username,password);
//身份验证
sendMailSession = Session.getInstance(props,sa);
//sendMailSession = Session.getInstance(props,null);
sendMailSession.setDebug(true);
MimeMessage newMessage = new MimeMessage(sendMailSession);
try
{
newMessage.setFrom(new InternetAddress(myfrom,"法律之星"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(myto));
}
catch(java.io.UnsupportedEncodingException ex)
{
System.out.println(ex.toString());
}
//设定邮件格式
newMessage.setSentDate(new Date());
System.out.println(htmlandtext+"邮件正文格式");
Multipart multipart = new MimeMultipart();
if (htmlandtext.equals("text"))
{
//获得文本格式的邮件
newMessage.setSubject(mysubject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(mytext);
multipart.addBodyPart(messageBodyPart);
}
else if(htmlandtext.equals("html"))
{
//设置邮件内容,将邮件body部分转化为HTML格式
newMessage.setSubject(mysubject,"gb2312");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mytext,"text/html;charset=gb2312"));
multipart.addBodyPart(messageBodyPart);
}
//将正文添加到邮件中
newMessage.setContent(multipart);
newMessage.saveChanges();
//transport = sendMailSession.getStore("pop3");
transport = sendMailSession.getTransport("smtp");
transport.connect(mysmtp,myport,username,password);
//transport.connect();
transport.send(newMessage,newMessage.getAllRecipients());
System.out.println("成功发送到"+myto+mytext);
return "ok";
}
catch(MessagingException m)
{
System.out.println(m.toString()+"失败");
//生成当前日期
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date dateTime= new Date();
String sDateTime=dateFormat.format(dateTime);
//生成日志文件
try
{
File filelog=new File(mailPathlog+"\\"+"mainlog.txt");
BufferedWriter out2=new BufferedWriter(new FileWriter(filelog.getPath(),true));
String newline = System.getProperty("line.separator");
out2.write(sDateTime+"/"+mysmtp+"/"+myfrom+"/"+myto+"/"+m.toString()+"/"+newline);
out2.close();
}
catch (IOException ex)
{
System.out.println(ex.toString());
}
return myto;
}
}
class SmtpAuthenticator extends Authenticator
{
//SMTP身份验证
public SmtpAuthenticator(String username,String password)
{
this.username=username;
this.password=password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new  PasswordAuthentication(this.username,this.password);
}
String username=null;
String password=null;
}
}

用JAVAMAIL发送邮件的一个简单例子相关推荐

  1. SAP MM采购定价过程的一个简单例子

    SAP MM采购定价过程的一个简单例子 本文以一个简单的例子阐述了SAP MM模块中采购定价的基本原理.本例中,假定采购订单里输入的是含税采购价,然后系统自动计算出物料最终的采购价格(含税价-税额=采 ...

  2. 一个简单例子:贫血模型or领域模型

    转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...

  3. KaTex的一个简单例子

    在网页中可以用KaTex显示数学公式,KaTex官网是https://katex.org 以下是KaTex的一个简单例子: 下载https://github.com/KaTeX/KaTeX/archi ...

  4. 单纯形法之人工变量法求解步骤:一个简单例子

    文章目录 人工变量法 1. 大M法 1.1. 题目 1.2. 转化为标准型 1.3. 添加人工变量 2. 两阶段法 2.1. 步骤 2.2. 题目 2.2.1. 转化为标准型 2.2.2. 添加人工变 ...

  5. 初次使用CTex时的一个简单例子

    初次使用CTex时的一个简单例子 1.初次使用Ctex时的例子 第一次使用 LATEX,在WinEdt编辑以下代码,然后使用PDFLaTeX生成PDF文档. \documentclass{articl ...

  6. 协方差检验用在什么地方_通过一个简单例子,通俗讲下协方差分析

    内容来自:"小白学统计"微信公众号,感谢作者授权. 临床中经常碰到这种设计:研究对象分为两组,接受不同治疗(如治疗组和安慰组),每组分别在治疗前和治疗后测量观察指标(如血压值).目 ...

  7. 什么是量子计算机?用一个简单例子来解释

    译者:王亮  作者:YK Sugi  原文:http://t.cn/EZAElk0 Hi,大家好! 不久前,我参观了加拿大温哥华的D-Wave Systems公司,这是一家制造前沿量子计算机的公司. ...

  8. 通过一个简单例子理解 RecyclerView.ItemDecoration

    一.前言 RecyclerView 是从5.0推出的 MD 风格的控件.RecyclerView 之前有 ListView.GridView,但是功能很有限,例如 ListView 只能实现垂直方向上 ...

  9. 词法分析程序 LEX和VC6整合使用的一个简单例子

    词法分析的理论知识不少,包括了正规式.正规文法.它们之间的转换以及确定的有穷自动机和不确定的有穷自动机等等... 要自己写一个词法分析器也不会很难,只要给出了最简的有穷自动机,就能很方便实现了,用if ...

最新文章

  1. 我要一颗原子弹 -- 开发者思维
  2. MySQL连不上,报Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
  3. JavaScript实现fisherYates洗牌算法(附完整源码)
  4. ssh服务、密钥登陆配置
  5. 【Ubuntu】ubuntu物理机安装方法:U盘安装
  6. vue 指令 v-once
  7. Redhat7离线安装mysql_linux 离线安装mysql7或者8
  8. GB2312汉字区位码、交换码和机内码转换方法(转)
  9. 攻城狮算保险--理财型,还是消费型?
  10. 函数类型+WINAPI+函数名
  11. 【ADNI】数据预处理(2)获取 subject slices
  12. C++实现鼠标左键点击关闭控制台
  13. 第11.25节 Python正则表达式编译re.compile及正则对象使用
  14. 一个网站直接跳转引起的一些思考
  15. 服务器安装sata固态硬盘吗,SATA接口固态硬盘安装教程
  16. 这2个PDF转Word免费不限页数工具很多人没用过
  17. 基于OTSU最大类间方差法的ROI分割、提取图像中的形状特征--面积、周长、离心率、zernike矩
  18. Micromedia Flash Player已终止一项可能不安全的操作解决办法
  19. PS-TS-PES-ES流结构分析
  20. Python爬虫实战三 | 蓝奏网盘抓取网盘链接信息

热门文章

  1. 音视频技术开发周刊 | 151
  2. 大牛书单 | 春节特辑
  3. 谁动了我的工作效率?大咖分享融合通信背后的技术案例
  4. C++封装、继承、多态
  5. 微服务架构日志集中化 安装 EFK (Fluentd ElasticSearch Kibana) 采集nginx日志
  6. nginx https http2
  7. RequestMethod的参数的含义是?
  8. Redis03_基础命令操作
  9. java基础---Calendar类
  10. 牛客网_PAT乙级_1025插入与归并(25)