javamail发送邮件(简单-->复杂)

来源:互联网 发布:淘宝买iphone怎么验货 编辑:程序博客网 时间:2024/06/05 18:41

javaMail可以很轻松实现邮件发送--读取邮件功能:需要下载javaMail和JAF的jar包,可以自己去网上下载!

1.一般的简单邮件--如纯文本超文本类型的,下面例子可以很多好的解决

//字符集 一般采用 ----GBK-----GB2312---utf-8//使用指定的base64方式编码,并指定编码内容的字符集是gb2312//text/plain(纯文本)和text/html(超文本)   纯文本换行---\r\n  超文本---<br>public static void main(String[] args) throws AddressException, MessagingException {//properties设置会话一些必要属性Properties pro = new Properties();//设置---服务器协议pro.put("mail.transport.protocol","smtp");//是否需要---认证--用户名和密码pro.put("mail.smtp.auth","true");//服务器--地址pro.put("mail.host","smtp.qq.com");//Session.getDefaultInstance(props, null);第二个参数-null说明不需要验证用户名和密码Session session = Session.getDefaultInstance(pro,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// TODO Auto-generated method stubreturn new PasswordAuthentication("***用户名****","*****密码*****");}});//打印---过程信息session.setDebug(true);//发送信息 协议的 实现类TransportTransport transport = session.getTransport();//信息实现类MimeMessageMessage msg = new MimeMessage(session);//设置 发送者msg.setFrom(new InternetAddress("*****发送邮箱地址*****"));//设置 邮件主题msg.setSubject("测试javaMail。。。。。");//设置邮件  正文msg.setContent("<a href='http://www.nala.com.cn'>NALA</a>欢迎您,请点击注册!","text/html;charset=gbk");//msg.setText("我的邮件系统,测试中。。。看到了说明成功了噢!<a href='http://www.nala.com.cn'>NALA</a>");//纯文本  直接这样就行了//transport.connect("smtp.qq.com",25,"用户名","密码");---连接,在此可不用写Transport.send(msg,new Address[]{new InternetAddress("***收件人地址***")});transport.close();}



2.带有 附件并有插入图片的邮件发送

//使用指定的base64方式编码,并指定编码内容的字符集是gb2312public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {Properties props = new Properties();//邮件  协议props.put("mail.transport.protocol", "smtp");//邮件 是否需要验证props.put("mail.smtp.auth","true");//邮件  服务器props.put("mail.host","smtp.qq.com");Session session = Session.getDefaultInstance(props, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// TODO Auto-generated method stubreturn new PasswordAuthentication("**用户名***","***密码***");}});session.setDebug(true);//发送 邮件----发送功能Transport transport = session.getTransport();//邮件的  消息Message msg = new MimeMessage(session);//邮件主题msg.setSubject("带--附件的邮件发送测试。。。。。。。。。");//邮件  接收者Address addTarget = new InternetAddress("**接收地址***");msg.addRecipient(Message.RecipientType.TO,addTarget);//邮件  发送者Address addFrom = new InternetAddress("**发送者地址**");msg.setFrom(addFrom);//设置  容器类Multipart mainPart = new MimeMultipart();//默认是 “MIXED”类型BodyPart body = new MimeBodyPart();//附件BodyPart body_ = new MimeBodyPart();//正文BodyPart bodyFile = new MimeBodyPart();//嵌入文本的图片//正文内容  设置---src='cid:IMG'---bodyFile.setHeader("Content-ID","<IMG>")   其中"<IMG>"的"<>"不能省,'cid:IMG'的'cid:'记住要加上body_.setContent("<a href='http://www.nala.com.cn'>NALA</a>欢迎您,请点击注册!<br><img src='cid:IMG' alt='正文嵌入图片'/>","text/html;charset=utf-8");mainPart.addBodyPart(body_);//附件  ----File file = new File("images/berty.jpg");body.setDataHandler(new DataHandler(new FileDataSource(file)));//文件-----一定要 设置,字符集,base64编码body.setFileName(MimeUtility.encodeText(file.getName(),"utf-8","B"));mainPart.addBodyPart(body);File file_ = new File("images/u=1846933279,1960524136&fm=21&gp=0.jpg");bodyFile.setDataHandler(new DataHandler(new FileDataSource(file_)));//文件-----一定要 设置,字符集及base64编码bodyFile.setFileName(MimeUtility.encodeText(file_.getName(),"GB2312","B"));bodyFile.setHeader("Content-ID","<IMG>");mainPart.addBodyPart(bodyFile);msg.setContent(mainPart);msg.saveChanges();//这种方式--发送邮件  在群发情况下 效率很高transport.send(msg, msg.getAllRecipients());transport.close();}


--------------------------------------自己不懂  可以去网上 找javamailAPI看看,很简单的--------------------------------


原创粉丝点击