免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1689 | 回复: 0
打印 上一主题 下一主题

java 发送邮件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-02 16:55 |只看该作者 |倒序浏览
import javax.mail.*;  import javax.mail.internet.*;  
import java.text.SimpleDateFormat;import java.util.*;  import javax.activation.*;    public class Mail {        public static void main(String[] args) {            Properties props = System.getProperties();          // 设置smtp服务器          props.setProperty("mail.smtp.host", "smtp.yicha.cn");          // 现在的大部分smpt都需要验证了          props.put("mail.smtp.auth", "true");            Session s = Session.getInstance(props);          // 为了查看运行时的信息          s.setDebug(true);          // 由邮件会话新建一个消息对象          MimeMessage message = new MimeMessage(s);          try {              // 发件人              InternetAddress from = new InternetAddress("lgzbj2006@yahoo.com.cn");              //message.setFrom(from);              // 收件人              InternetAddress to = new InternetAddress("renyiyong@yicha.cn");              InternetAddress to2 = new InternetAddress("liuhongwei@yicha.cn");            InternetAddress[] totest={to,to2};            //message.setRecipient(Message.RecipientType.TO, to);              message.setRecipients(Message.RecipientType.TO,totest);            // 邮件标题              message.setSubject("test");              String content = "测试内容";              // 邮件内容,也可以使纯文本"text/plain"              message.setContent(content, "text/html;charset=GBK");              message.getReceivedDate();              /****下面代码是发送附件****** */             String fileName = "d:\\hello.txt";             MimeBodyPart messageBodyPart = new MimeBodyPart();             SimpleDateFormat stringdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");            messageBodyPart.setText("问答元搜索,解析百度页面出现问题,请及时处理\r\n"+"yicha.cn\r\n"+stringdate.format(new Date()));             Multipart multipart = new MimeMultipart();             multipart.addBodyPart(messageBodyPart);              messageBodyPart = new MimeBodyPart();             DataSource source = new FileDataSource(fileName);             messageBodyPart.setDataHandler(new DataHandler(source));             messageBodyPart.setFileName(fileName);             multipart.addBodyPart(messageBodyPart);              message.setContent(multipart);                           message.saveChanges();              Transport transport = s.getTransport("smtp");              // smtp验证,就是你用来发邮件的邮箱用户名密码              transport.connect("smtp.yicha.cn", "renyiyong@yicha.cn", "yicha1");              // 发送              transport.sendMessage(message, message.getAllRecipients());              transport.close();            } catch (Exception e) {              e.printStackTrace();          }        }    }  需要两个jar包的下载地址:mail.jar 下载地址:
http://java.sun.com/products/javamail/downloads/index.html
activation.jar 下载地址:
http://java.sun.com/products/javabeans/jaf/downloads/index.html
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104127/showart_2109617.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP