javamail,java发email,发邮件,jaf,activation,j2ee5冲突问题解决

首先要引mail.jar和activation.jar包

下载地址为: javamail:http://java.sun.com/products/javamail/downloads/index.html

                 JAF:http://java.sun.com/javase/technologies/desktop/javabeans/jaf/downloads/index.html

        下载后把.zip解压,然后把mail.jar和activation.jar复制到项目下的web-info/lib下

        值得一说的是java ee 5和这个mail.jar和activation.jar有冲突 这个类在j2ee1.4下运行完全没有问题,但是如果在java ee 5下运行会报类找不到的错误,解决办法就是把myeclipse下的java ee 5的mail.jar和activation.jar干掉

        进到

         E:/Program Files/MyEclipse 6.5/myeclipse/eclipse/plugins/com.genuitec.eclipse.j2eedt.core_6.5.0.zmyeclipse650200806/data/libraryset/EE_5 这个路径里,可以看到javaee.jar,用rar把这个文件打开,然后进到javax文件夹里,删除mail.jar和activation.jar,然后再运行这个类,一切正常 发邮件的类

 

Java代码 复制代码  收藏代码
  1. package javamail;      
  2.      
  3. import java.util.Properties;      
  4.      
  5. import javax.activation.DataHandler;      
  6. import javax.activation.FileDataSource;      
  7. import javax.mail.Address;      
  8. import javax.mail.BodyPart;      
  9. import javax.mail.Message;      
  10. import javax.mail.Multipart;      
  11. import javax.mail.Session;      
  12. import javax.mail.Transport;      
  13. import javax.mail.internet.InternetAddress;      
  14. import javax.mail.internet.MimeBodyPart;      
  15. import javax.mail.internet.MimeMessage;      
  16. import javax.mail.internet.MimeMultipart;      
  17.      
  18.      
  19. public class SendMail {      
  20.      
  21.  private MimeMessage mimeMsg; // MIME邮件对象      
  22.      
  23.  private Session session; // 邮件会话对象      
  24.      
  25.  private Properties props; // 系统属性      
  26.      
  27.  private boolean needAuth = false// smtp是否需要认证      
  28.      
  29.  private String username = ""// smtp认证用户名和密码      
  30.      
  31.  private String password = "";      
  32.      
  33.  private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象      
  34.      
  35.  /**    
  36.   *     
  37.   *     
  38.   *     
  39.   */     
  40.      
  41.  public SendMail() {      
  42.      
  43. //  setSmtpHost(getConfig.mailHost);// 如果没有指定邮件服务器,就从getConfig类中获取      
  44.   setSmtpHost("smtp.126.com");// 如果没有指定邮件服务器,就从getConfig类中获取      
  45.      
  46.   createMimeMessage();      
  47.      
  48.  }      
  49.      
  50.  public SendMail(String smtp) {      
  51.      
  52.   setSmtpHost(smtp);      
  53.      
  54.   createMimeMessage();      
  55.      
  56.  }      
  57.      
  58.  /**    
  59.   *     
  60.   * @param hostName    
  61.   *            String    
  62.   *     
  63.   */     
  64.      
  65.  public void setSmtpHost(String hostName) {      
  66.      
  67.   System.out.println("设置系统属性:mail.smtp.host = " + hostName);      
  68.      
  69.   if (props == null)      
  70.    props = System.getProperties(); // 获得系统属性对象      
  71.      
  72.   props.put("mail.smtp.host", hostName); // 设置SMTP主机      
  73.      
  74.  }      
  75.      
  76.  /**    
  77.   *     
  78.   * @return boolean    
  79.   *     
  80.   */     
  81.      
  82.  public boolean createMimeMessage()      
  83.      
  84.  {      
  85.      
  86.   try {      
  87.      
  88.    System.out.println("准备获取邮件会话对象!");      
  89.      
  90.    session = Session.getDefaultInstance(props, null); // 获得邮件会话对象      
  91.      
  92.   }      
  93.      
  94.   catch (Exception e) {      
  95.      
  96.    System.err.println("获取邮件会话对象时发生错误!" + e);      
  97.      
  98.    return false;      
  99.      
  100.   }      
  101.      
  102.   System.out.println("准备创建MIME邮件对象!");      
  103.      
  104.   try {      
  105.      
  106.    mimeMsg = new MimeMessage(session); // 创建MIME邮件对象      
  107.      
  108.    mp = new MimeMultipart();      
  109.      
  110.    return true;      
  111.      
  112.   }      
  113.      
  114.   catch (Exception e) {      
  115.      
  116.    System.err.println("创建MIME邮件对象失败!" + e);      
  117.      
  118.    return false;      
  119.      
  120.   }      
  121.      
  122.  }      
  123.      
  124.  /**    
  125.   *     
  126.   * @param need    
  127.   *            boolean    
  128.   *     
  129.   */     
  130.      
  131.  public void setNeedAuth(boolean need) {      
  132.      
  133.   System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);      
  134.      
  135.   if (props == null)      
  136.    props = System.getProperties();      
  137.      
  138.   if (need) {      
  139.      
  140.    props.put("mail.smtp.auth""true");      
  141.      
  142.   } else {      
  143.      
  144.    props.put("mail.smtp.auth""false");      
  145.      
  146.   }      
  147.      
  148.  }      
  149.      
  150.  /**    
  151.   *     
  152.   * @param name    
  153.   *            String    
  154.   *     
  155.   * @param pass    
  156.   *            String    
  157.   *     
  158.   */     
  159.      
  160.  public void setNamePass(String name, String pass) {      
  161.      
  162.   username = name;      
  163.      
  164.   password = pass;      
  165.      
  166.  }      
  167.      
  168.  /**    
  169.   *     
  170.   * @param mailSubject    
  171.   *            String    
  172.   *     
  173.   * @return boolean    
  174.   *     
  175.   */     
  176.      
  177.  public boolean setSubject(String mailSubject) {      
  178.      
  179.   System.out.println("设置邮件主题!");      
  180.      
  181.   try {      
  182.      
  183.    mimeMsg.setSubject(mailSubject);      
  184.      
  185.    return true;      
  186.      
  187.   }      
  188.      
  189.   catch (Exception e) {      
  190.      
  191.    System.err.println("设置邮件主题发生错误!");      
  192.      
  193.    return false;      
  194.      
  195.   }      
  196.      
  197.  }      
  198.      
  199.  /**    
  200.   *     
  201.   * @param mailBody    
  202.   *            String    
  203.   *     
  204.   */     
  205.      
  206.  public boolean setBody(String mailBody) {      
  207.      
  208.   try {      
  209.      
  210.    BodyPart bp = new MimeBodyPart();      
  211.      
  212.    bp.setContent(      
  213.      "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"     
  214.        + mailBody, "text/html;charset=GB2312");      
  215.      
  216.    mp.addBodyPart(bp);      
  217.      
  218.    return true;      
  219.      
  220.   }      
  221.      
  222.   catch (Exception e) {      
  223.      
  224.    System.err.println("设置邮件正文时发生错误!" + e);      
  225.      
  226.    return false;      
  227.      
  228.   }      
  229.      
  230.  }      
  231.      
  232.  /**    
  233.   *     
  234.   * @param name    
  235.   *            String    
  236.   *     
  237.   * @param pass    
  238.   *            String    
  239.   *     
  240.   */     
  241.      
  242.  public boolean addFileAffix(String filename) {      
  243.      
  244.   System.out.println("增加邮件附件:" + filename);      
  245.      
  246.   try {      
  247.      
  248.    BodyPart bp = new MimeBodyPart();      
  249.      
  250.    FileDataSource fileds = new FileDataSource(filename);      
  251.      
  252.    bp.setDataHandler(new DataHandler(fileds));      
  253.      
  254.    bp.setFileName(fileds.getName());      
  255.      
  256.    mp.addBodyPart(bp);      
  257.      
  258.    return true;      
  259.      
  260.   }      
  261.      
  262.   catch (Exception e) {      
  263.      
  264.    System.err.println("增加邮件附件:" + filename + "发生错误!" + e);      
  265.      
  266.    return false;      
  267.      
  268.   }      
  269.      
  270.  }      
  271.      
  272.  /**    
  273.   *     
  274.   * @param name    
  275.   *            String    
  276.   *     
  277.   * @param pass    
  278.   *            String    
  279.   *     
  280.   */     
  281.      
  282.  public boolean setFrom(String from) {      
  283.      
  284.   System.out.println("设置发信人!");      
  285.      
  286.   try {      
  287.      
  288.    mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人      
  289.      
  290.    return true;      
  291.      
  292.   }      
  293.      
  294.   catch (Exception e)      
  295.      
  296.   {      
  297.    return false;      
  298.   }      
  299.      
  300.  }      
  301.      
  302.  /**    
  303.   *     
  304.   * @param name    
  305.   *            String    
  306.   *     
  307.   * @param pass    
  308.   *            String    
  309.   *     
  310.   */     
  311.      
  312.  public boolean setTo(String to) {      
  313.      
  314.   if (to == null)      
  315.    return false;      
  316.      
  317.   try {      
  318.      
  319.    mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress      
  320.      .parse(to));      
  321.      
  322.    return true;      
  323.      
  324.   }      
  325.      
  326.   catch (Exception e)      
  327.      
  328.   {      
  329.    return false;      
  330.   }      
  331.      
  332.  }      
  333.      
  334.  /**    
  335.   *     
  336.   * @param name    
  337.   *            String    
  338.   *     
  339.   * @param pass    
  340.   *            String    
  341.   *     
  342.   */     
  343.      
  344.  public boolean setCopyTo(String copyto)      
  345.      
  346.  {      
  347.      
  348.   if (copyto == null)      
  349.    return false;      
  350.      
  351.   try {      
  352.      
  353.    mimeMsg.setRecipients(Message.RecipientType.CC,      
  354.      (Address[]) InternetAddress.parse(copyto));      
  355.      
  356.    return true;      
  357.      
  358.   }      
  359.      
  360.   catch (Exception e)      
  361.      
  362.   {      
  363.    return false;      
  364.   }      
  365.      
  366.  }      
  367.      
  368.  /**    
  369.   *     
  370.   * @param name    
  371.   *            String    
  372.   *     
  373.   * @param pass    
  374.   *            String    
  375.   *     
  376.   */     
  377.      
  378.  public boolean sendout()      
  379.      
  380.  {      
  381.      
  382.   try {      
  383.      
  384.    mimeMsg.setContent(mp);      
  385.      
  386.    mimeMsg.saveChanges();      
  387.      
  388.    System.out.println("正在发送邮件....");      
  389.      
  390.    Session mailSession = Session.getInstance(props, null);      
  391.      
  392.    Transport transport = mailSession.getTransport("smtp");      
  393.      
  394.    transport.connect((String) props.get("mail.smtp.host"), username,      
  395.      password);      
  396.      
  397.    transport.sendMessage(mimeMsg, mimeMsg      
  398.      .getRecipients(Message.RecipientType.TO));      
  399.      
  400.    // transport.send(mimeMsg);      
  401.      
  402.    System.out.println("发送邮件成功!");      
  403.      
  404.    transport.close();      
  405.      
  406.    return true;      
  407.      
  408.   }      
  409.      
  410.   catch (Exception e)      
  411.      
  412.   {      
  413.      
  414.    System.err.println("邮件发送失败!" + e);      
  415.      
  416.    return false;      
  417.      
  418.   }      
  419.      
  420.  }      
  421.      
  422.  /**    
  423.   *     
  424.   * Just do it as this    
  425.   *     
  426.   */     
  427.      
  428.  public static void main(String[] args) {      
  429.      
  430.   String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"     
  431.     +      
  432.      
  433.     "<div align=center><a href=http://www.csdn.net> csdn </a></div>";      
  434.      
  435.   SendMail themail = new SendMail("smtp.126.com");      
  436.      
  437.   themail.setNeedAuth(true);      
  438.      
  439.   if (themail.setSubject("Title") == false)      
  440.    return;      
  441.      
  442.   if (themail.setBody(mailbody) == false)      
  443.    return;      
  444.      
  445.   if (themail.setTo("toEmailUser") == false)      
  446.    return;      
  447.      
  448.   if (themail.setFrom("yourEmailUser") == false)      
  449.    return;      
  450.      
  451.   if (themail.addFileAffix("g://test.txt") == false)      
  452.    return;      
  453.      
  454.   themail.setNamePass("mailUsername""mailPassword");      
  455.      
  456.   if (themail.sendout() == false)      
  457.    return;      
  458.      
  459.  }      
  460.      
  461. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值