java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件
来源:互联网 发布:外国人上淘宝感觉 编辑:程序博客网 时间:2024/05/16 06:14
全栈工程师开发手册 (作者:栾鹏)
java教程全解
java使用javax.mail包发送电子邮件。这个实例可发送多附件。
这里使用163邮箱进行测试。可以设置账号、密码、主题、文本内容、附件
测试代码
public static void main(String[] args){ Mail sendmail = new Mail(); sendmail.setHost("smtp.163.com"); sendmail.setUserName("aaaaaaaaa@163.com"); sendmail.setPassWord("aaaaaaaaa"); sendmail.setTo("aaaaaaaaa@163.com"); sendmail.setFrom("aaaaaaaaa@163.com"); sendmail.setSubject("你好,这是测试!"); sendmail.setContent("你好这是一个带多附件的测试!"); //Mail sendmail = new Mail("aaaaaaaaa@163.com","aaaaaaaaa@163.com","smtp.163.com","aaaaaaaaa","aaaaaaaaa","你好","胃,你好吗?"); sendmail.attachfile("D:\\test.jpg"); sendmail.attachfile("D:\\test.mp4"); sendmail.sendMail(); }
邮件发送类的实现
package com.lp.app.net;import java.util.*;import java.io.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;String to = "";//收件人String from = "";//发件人String host = "";//smtp主机String username = "" ;String password = "" ;String filename = "";//附件文件名String subject = "";//邮件主题String content = "";//邮件正文Vector file = new Vector();//附件文件集合public Mail(){}//构造器,提供直接的参数传入public Mail(String to,String from,String smtpServer,String username,String password,String subject,String content){ this.to = to; this.from = from; this.host = smtpServer; this.username = username; this.password = password; this.subject = subject; this.content = content;}//设置邮件服务器地址public void setHost(String host){ this.host = host;}//设置登录服务器校验密码public void setPassWord(String pwd){ this.password = pwd;}//设置登录服务器校验用户public void setUserName(String usn){ this.username = usn;}//设置邮件发送目的邮箱public void setTo(String to){ this.to = to;}//设置邮件发送源邮箱public void setFrom(String from){ this.from = from;}//设置邮件主题public void setSubject(String subject){ this.subject = subject;}//设置邮件内容public void setContent(String content){ this.content = content;}//把主题转换为中文public String transferChinese(String strText){ try{ strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B"); }catch(Exception e){ e.printStackTrace(); } return strText;}//往附件组合中添加附件public void attachfile(String fname){ file.addElement(fname);}//发送邮件public boolean sendMail(){ //构造mail session Properties props = System.getProperties(); props.put("mail.smtp.host",host); props.put("mail.smtp.auth","true"); Session session=Session.getDefaultInstance(props, new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username,password); } }); try { //构造MimeMessage 并设定基本的值 MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address={new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO,address); subject = transferChinese(subject); msg.setSubject(subject); //构造Multipart Multipart mp = new MimeMultipart(); //向Multipart添加正文 MimeBodyPart mbpContent = new MimeBodyPart(); mbpContent.setText(content); //向MimeMessage添加(Multipart代表正文) mp.addBodyPart(mbpContent); //向Multipart添加附件 Enumeration efile=file.elements(); while(efile.hasMoreElements()){ MimeBodyPart mbpFile = new MimeBodyPart(); filename=efile.nextElement().toString(); FileDataSource fds = new FileDataSource(filename); mbpFile.setDataHandler(new DataHandler(fds)); mbpFile.setFileName(fds.getName()); //向MimeMessage添加(Multipart代表附件) mp.addBodyPart(mbpFile); } file.removeAllElements(); //向Multipart添加MimeMessage msg.setContent(mp); msg.setSentDate(new Date()); //发送邮件 Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex=mex.getNextException())!=null){ ex.printStackTrace(); } return false; } return true; }}
本文只试验了163邮件,读者可以按下面的主机地址,试验其他邮箱。但并不是每种邮箱都能成功。
gmail(google.com)
POP3服务器地址:pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)
21cn.com:
POP3服务器地址:pop.21cn.com(端口:110)
SMTP服务器地址:smtp.21cn.com(端口:25)
sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110)
SMTP服务器地址:smtp.sina.com.cn(端口:25)
tom.com:
POP3服务器地址:pop.tom.com(端口:110)
SMTP服务器地址:smtp.tom.com(端口:25)
163.com:
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)
263.net:
POP3服务器地址:pop3.263.net(端口:110)
SMTP服务器地址:smtp.263.net(端口:25)
yahoo.com:
POP3服务器地址:pop.mail.yahoo.com
SMTP服务器地址:smtp.mail.yahoo.com
263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110)
SMTP服务器地址:smtp.263.net.cn(端口:25)
Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110)
SMTP服务器地址:SMTP.foxmail.com(端口:25)
sinaVIP
POP3服务器:pop3.vip.sina.com (端口:110)
SMTP服务器:smtp.vip.sina.com (端口:25)
sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110)
SMTP服务器地址:smtp.sohu.com(端口:25)
etang.com:
POP3服务器地址:pop.etang.com
SMTP服务器地址:smtp.etang.com
x263.net:
POP3服务器地址:pop.x263.net(端口:110)
SMTP服务器地址:smtp.x263.net(端口:25)
yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995)
SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587)
雅虎邮箱POP3的SSL不启用端口为110,POP3的SSL启用端口995;SMTP的SSL不启用端口为25,SMTP的SSL启用端口为465
QQ邮箱 QQ企业邮箱
POP3服务器地址:pop.qq.com(端口:110) POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995)
SMTP服务器地址:smtp.qq.com (端口:25) SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
SMTP服务器需要身份验证
126邮箱 HotMail
POP3服务器地址:pop.126.com(端口:110) POP3服务器地址:pop.live.com (端口:995)
SMTP服务器地址:smtp.126.com(端口:25) SMTP服务器地址:smtp.live.com (端口:587)
china.com: 139邮箱
POP3服务器地址:pop.china.com(端口:110) POP3服务器地址:POP.139.com(端口:110)
SMTP服务器地址:smtp.china.com(端口:25) SMTP服务器地址:SMTP.139.com(端口:25)
- java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件
- 使用javax.mail 发送邮件 带附件
- Java使用javax.mail.jar发送邮件并允许发送附件
- Java-使用java mail技术发送电子邮件
- 发送电子邮件 java mail
- JAVA mail发送电子邮件
- 使用mail发送附件
- 使用Java发送邮件(javax.mail)
- 【JAVA】使用javax.mail发送邮件
- Java使用javax.mail发送邮件
- java使用javax.mail.jar发送邮件
- 【JAVA】使用javax.mail发送邮件
- javax.mail发送邮件带附件
- javax.mail发送邮件(带附件)
- Java mail 实现发送电子邮件
- java Mail发送附件邮件
- Java发送邮件javax.mail
- Java发送邮件javax.mail
- 传歌到手机So easy!盘点四款自带传歌功能的音乐软件
- Google“白日梦”的真正受益者,应该不是暴风而是小米们
- 一只爬虫带你看世界【9】
- COALESCE(SUM(total),0) 函数mysql
- 【安卓小笔记】BottomNavigationView和ViewPager的使用
- java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件
- 开始使用travis ci
- itop4412的平台驱动注册
- 分支应用(条件语句)
- java 解析excel文件内容
- hashCode与equals的作用与区别及应当注意的细节
- Python中进程和线程的总体区别
- 62. Unique Paths
- SPA单页面应用