[Java] 邮件发送类 →→→→→进入此内容的聊天室

来自 , 2020-01-08, 写在 Java, 查看 101 次.
URL http://www.code666.cn/view/2d6cc4b2
  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import javax.activation.*;
  5.  
  6. /**
  7.  *
  8.  * 发送邮件
  9.  */
  10. public class SMTPSender {
  11.  
  12.         private MimeMessage mimeMsg; // MIME邮件对象
  13.  
  14.         private Session session; // 邮件会话对象
  15.         private Properties props; // 系统属性
  16.         private boolean needAuth = false; // smtp是否需要认证
  17.  
  18.         private String username = ""; // smtp认证用户名和密码
  19.         private String password = "";
  20.  
  21.         private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
  22.  
  23.         public SMTPSender(String smtp) {
  24.                 setSmtpHost(smtp);
  25.                 createMimeMessage();
  26.         }
  27.  
  28.         public void setHost(String smtp) {
  29.                 setSmtpHost(smtp);
  30.                 createMimeMessage();
  31.  
  32.         }
  33.  
  34.         /**
  35.          *
  36.          * @param hostName
  37.          */
  38.         public void setSmtpHost(String hostName) {
  39.                 System.out.println("设置系统属性:mail.smtp.host = " + hostName);
  40.                 if (props == null)
  41.                         props = System.getProperties(); // 获得系统属性对象
  42.  
  43.                 props.put("mail.smtp.host", hostName); // 设置SMTP主机
  44.         }
  45.  
  46.         /**
  47.          *
  48.          * @return
  49.          */
  50.         public boolean createMimeMessage() {
  51.                 try {
  52.                         System.out.println("准备获取邮件会话对象!");
  53.                         session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
  54.                 } catch (Exception e) {
  55.                         System.err.println("获取邮件会话对象时发生错误!" + e);
  56.                         return false;
  57.                 }
  58.  
  59.                 System.out.println("准备创建MIME邮件对象!");
  60.                 try {
  61.                         mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
  62.                         mp = new MimeMultipart();
  63.  
  64.                         return true;
  65.                 } catch (Exception e) {
  66.                         System.err.println("创建MIME邮件对象失败!" + e);
  67.                         return false;
  68.                 }
  69.         }
  70.  
  71.         /**
  72.          *
  73.          * @param need
  74.          */
  75.         public void setNeedAuth(boolean need) {
  76.                 System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
  77.                 if (props == null)
  78.                         props = System.getProperties();
  79.  
  80.                 if (need) {
  81.                         props.put("mail.smtp.auth", "true");
  82.                 } else {
  83.                         props.put("mail.smtp.auth", "false");
  84.                 }
  85.         }
  86.  
  87.         /**
  88.          *
  89.          * @param name
  90.          * @param pass
  91.          */
  92.         public void setNamePass(String name, String pass) {
  93.                 username = name;
  94.                 password = pass;
  95.         }
  96.  
  97.         /**
  98.          *
  99.          * @param mailSubject
  100.          * @return
  101.          */
  102.         public boolean setSubject(String mailSubject) {
  103.                 System.out.println("设置邮件主题!");
  104.                 try {
  105.                         mimeMsg.setSubject(mailSubject);
  106.                         return true;
  107.                 } catch (Exception e) {
  108.                         System.err.println("设置邮件主题发生错误!");
  109.                         return false;
  110.                 }
  111.         }
  112.  
  113.         /**
  114.          *
  115.          * @param mailBody
  116.          * @return
  117.          */
  118.         public boolean setBody(String mailBody) {
  119.                 try {
  120.                         BodyPart bp = new MimeBodyPart();
  121.                         bp.setContent("" + mailBody, "text/html;charset=GB2312");
  122.                         mp.addBodyPart(bp);
  123.  
  124.                         return true;
  125.                 } catch (Exception e) {
  126.                         System.err.println("设置邮件正文时发生错误!" + e);
  127.                         return false;
  128.                 }
  129.         }
  130.  
  131.         /**
  132.          *
  133.          * @param filename
  134.          * @return
  135.          */
  136.         public boolean addFileAffix(String filename) {
  137.  
  138.                 System.out.println("增加邮件附件:" + filename);
  139.                 try {
  140.                         BodyPart bp = new MimeBodyPart();
  141.                         FileDataSource fileds = new FileDataSource(filename);
  142.                         bp.setDataHandler(new DataHandler(fileds));
  143.                         bp.setFileName(fileds.getName());
  144.  
  145.                         mp.addBodyPart(bp);
  146.  
  147.                         return true;
  148.                 } catch (Exception e) {
  149.                         System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
  150.                         return false;
  151.                 }
  152.         }
  153.  
  154.         /**
  155.          *
  156.          * @param from
  157.          * @return
  158.          */
  159.         public boolean setFrom(String from) {
  160.                 System.out.println("设置发信人!");
  161.                 try {
  162.                         mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
  163.                         return true;
  164.                 } catch (Exception e) {
  165.                         return false;
  166.                 }
  167.         }
  168.  
  169.         /**
  170.          *
  171.          * @param to
  172.          * @return
  173.          */
  174.         public boolean setTo(String to) {
  175.                 if (to == null)
  176.                         return false;
  177.                 try {
  178.                         mimeMsg.setRecipients(Message.RecipientType.TO,
  179.                                         InternetAddress.parse(to));
  180.                         return true;
  181.                 } catch (Exception e) {
  182.                         return false;
  183.                 }
  184.  
  185.         }
  186.  
  187.         /**
  188.          *
  189.          * @param copyto
  190.          * @return
  191.          */
  192.         public boolean setCopyTo(String copyto) {
  193.                 if (copyto == null)
  194.                         return false;
  195.                 try {
  196.                         mimeMsg.setRecipients(Message.RecipientType.CC,
  197.                                         (Address[]) InternetAddress.parse(copyto));
  198.                         return true;
  199.                 } catch (Exception e) {
  200.                         return false;
  201.                 }
  202.         }
  203.  
  204.         /**
  205.          *
  206.          * @return
  207.          */
  208.         public boolean sendout() {
  209.                 try {
  210.                         mimeMsg.setContent(mp);
  211.                         mimeMsg.saveChanges();
  212.                         System.out.println("正在发送邮件....");
  213.  
  214.                         Session mailSession = Session.getInstance(props, null);
  215.                         Transport transport = mailSession.getTransport("smtp");
  216.                         transport.connect((String) props.get("mail.smtp.host"), username,
  217.                                         password);
  218.                         transport.sendMessage(mimeMsg,
  219.                                         mimeMsg.getRecipients(Message.RecipientType.TO));
  220.                         // transport.send(mimeMsg);
  221.  
  222.                         System.out.println("发送邮件成功!");
  223.                         transport.close();
  224.  
  225.                         return true;
  226.                 } catch (Exception e) {
  227.                         System.err.println("邮件发送失败!" + e);
  228.                         return false;
  229.                 }
  230.         }
  231.  
  232.         /**
  233.          *
  234.          * @param toemail
  235.          * @param title
  236.          * @param msg
  237.          * @return
  238.          */
  239.         public boolean initMail(String toemail, String title, String msg) {
  240.                 String mailbody = msg;
  241.                 System.out.println("toemail:" + toemail);
  242.  
  243.                 SMTPSender themail = new SMTPSender(mailbody);
  244.                 themail.setHost("smtp.163.com");
  245.                 themail.setNeedAuth(true);
  246.  
  247.                 if (themail.setSubject(title) == false) {
  248.                         System.out.println("setsubject fail");
  249.                 }
  250.  
  251.                 if (themail.setBody(mailbody) == false) {
  252.                         System.out.println("setbody fail");
  253.                 }
  254.  
  255.                 if (themail.setTo(toemail) == false) {
  256.                         System.out.println("setto fail");
  257.                 }
  258.  
  259.                 if (themail.setFrom("xxx@163.com") == false) {
  260.                         System.out.println("setfrom fail");
  261.                         themail.setNamePass("xxx", "xxx");
  262.                 }
  263.  
  264.                 if (themail.sendout() == false) {
  265.                         return false;
  266.                 }
  267.  
  268.                 return true;
  269.         }
  270. }

回复 "邮件发送类"

这儿你可以回复上面这条便签

captcha