java 发送邮件的实例代码(可移植)

2016-02-19 11:04 4 1 收藏

下面图老师小编跟大家分享一个简单易学的java 发送邮件的实例代码(可移植)教程,get新技能是需要行动的,喜欢的朋友赶紧收藏起来学习下吧!

【 tulaoshi.com - 编程语言 】

以一个web项目为例,代码是可以移植的

首先要导入mail.jar包,然后创建自己的类

1:HTMLSender类

代码如下:

package com.txq.mail;

 import java.util.Properties;

 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.NoSuchProviderException;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
 import javax.mail.internet.MimeUtility;

 /**
  *
  * @author Administrator
  *
  */
 public class HTMLSender {
     private MimeMessage message;
     private Properties props;
     private Session session;
     private String name = "";
     private String password = "";

     public HTMLSender(String host,String name,String password)
     {
         this.name = name;
         this.password = password;
         props = System.getProperties();

         props.put("mail.smtp.host", host);
         props.put("mail.smtp.auth","true");
         MyAuthenticator auth = new MyAuthenticator(name,password);
         session = Session.getDefaultInstance(props,auth);

         message = new MimeMessage(session);
         new MimeMultipart();
     }

     public void setFrom(String from)
     {
         try
         {
             message.setFrom(new InternetAddress(from));
         }
         catch (AddressException e)
         {
             e.printStackTrace();
         }
         catch (MessagingException e)
         {
             e.printStackTrace();
         }
     }

     public void setTo(String to)
     {
         try
         {
             message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
         }
         catch(AddressException e)
         {
             e.printStackTrace();
         }
         catch(MessagingException e)
         {
             e.printStackTrace();
         }
     }

     public void setSubject(String subject)
     {
         try
         {
             message.setSubject(subject);
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
     }

     public void setContect(String content)
     {
         try
         {
             message.setContent(content,"text/html;charset=utf-8");
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
     }

     public boolean send()
     {
         try
         {
             Transport transport = session.getTransport("smtp");
             transport.connect((String)props.get("mail.smtp.host"),name,password);
             transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
             transport.close();
             return true;
         }
         catch(NoSuchProviderException e)
         {
             e.printStackTrace();
             return false;
         }
         catch(MessagingException e)
         {
             e.printStackTrace();
             return false;
         }
     }
 }

2:MyAuthenticator类

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

代码如下:

package com.txq.mail;

 import javax.mail.Authenticator;
 import javax.mail.PasswordAuthentication;

 
 public class MyAuthenticator extends Authenticator{
     String name;
     String password;
     public MyAuthenticator(String name,String pasword)
     {
         this.name = name;
         this.password = pasword;
         getPasswordAuthentication();
     }
     protected PasswordAuthentication getPasswordAuthentication()
     {
         return new PasswordAuthentication(name,password);
     }
 }

3:MailAction类

代码如下:

package com.txq.action;

 import java.io.PrintWriter;

 import com.opensymphony.xwork2.ActionSupport;
 import com.txq.mail.HTMLSender;

 public class MailAction extends ActionSupport {
     private String email;
     public String sendEmail(){
         //代理主机-新浪
         String host = "smtp.sina.cn";
         //帐号-新浪帐号
         String name = "followmusic";
         //密码-新浪密码
         String password = "followmusic";
         //创建发送邮件对象
         HTMLSender mail = new HTMLSender(host,name,password);
         //发邮帐号
         mail.setFrom("followmusic@sina.cn");
         //收邮帐号
         mail.setTo("youxiangzhanghe@qq.com");
         //邮件主题
         mail.setSubject("淘兴趣");
         //邮件内容
         mail.setContect("你的好友邀请你来淘兴趣~");

         if(mail.send())
         {
             return SUCCESS;
         }
         else
         {
             return "error";
         }
     }
     public String getEmail() {
         return email;
     }
     public void setEmail(String email) {
         this.email = email;
     }
 }

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

来源:http://www.tulaoshi.com/n/20160219/1596464.html

延伸阅读
标签: 电脑入门
步骤一、就以word2003为例吧。不管是什么版本的word,电脑必须是连接了网络的情形下才能发送邮件。打开word2003版本。然后在菜单看选择,然后在下方选择即可。如图所示: 步骤二、现在我们选择电子邮件之后会出现一个邮件的窗口,上面输入:收件、抄送、主题的信息,然后在下面的文档上输入文本内容。输入完成后即可点击最下方的,在下面先...
标签: ASP
  这个功能要装上WINDOWS NT OPTON PACK 的SMTP SERVICE。 首先 Set mail = Server.CreateObject("CDONTS.NewMail")以呼叫SMTP Service的元件,然后设定: mail.To = "收件者E-Mail地址" mail.From = "寄件者的E-Mail地址" mail.Subject = "主旨" mail.Body = "E-mail内文" 最后Mail.S...
 1.打开各种系统工具            2.定时关机(重启、睡眠未实现 请参照上面两个文章自行扩展)            3.简单文件操作 代码如下: [java]  package com.cxy.f;  import java.awt.Image;  import java.aw...
代码如下: import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.event.ActionEvent; imp...
代码如下: package com.gpdi.action; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class WordsStatistics {     class Obj {         int count ;     ...

经验教程

97

收藏

45
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部