当我在Java中实例化一个SimpleEmail类时,为什么会收到这个异常? [英] Why am I getting this exception when I instantiate a SimpleEmail class in Java?

查看:103
本文介绍了当我在Java中实例化一个SimpleEmail类时,为什么会收到这个异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的就是用Java发送电子邮件。这是我发现的例子:

  import org.apache.commons.mail.SimpleEmail; 

public class Email {

public static void sendMessage(String emailaddress,String subject,String body){
try {
SimpleEmail email = new SimpleEmail ();
email.setHostName(valid ip address here);
email.addTo(emailaddress);
email.setFrom(noreply@example.com,无回覆);
email.setSubject(subject);
email.setMsg(body);
email.send();
} catch(Exception ex){
ex.printStackTrace();
}
}
}

我立即得到以下异常在 SimpleEmail email = new SimpleEmail(); 行:

  java我有一个JAR项目(使用Netbeans):
commons-email-1.2.jar



我做错了什么



谢谢。

解决方案

不知道为什么SimpleEmail没有工作。但是这样做:

  import javax.mail。*; 
import javax.mail.internet。*;

import java.util.Properties;

public class Email {

public static void sendMessage(String emailaddress,String subject,String body){
try {
属性props = new Properties ();
props.setProperty(mail.transport.protocol,smtp);
props.setProperty(mail.host,myhost);

会话mailSession = Session.getDefaultInstance(道具,null);
运输transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject(测试javamail plain);
message.setContent(这是一个测试,text / plain);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(test@example.com));

transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
}

感谢您的建议。 / p>

All I want to do is send an email in Java. Here is the example I found:

import org.apache.commons.mail.SimpleEmail;

public class Email {

    public static void sendMessage(String emailaddress, String subject, String body) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setHostName("valid ip address here");
            email.addTo(emailaddress);
            email.setFrom("noreply@example.com", "No reply");
            email.setSubject(subject);
            email.setMsg(body);
            email.send();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

I immediately get the following exception on the SimpleEmail email = new SimpleEmail(); line:

java.lang.ClassNotFoundException: org.apache.commons.mail.SimpleEmail

I have the following JAR in my project (using Netbeans): commons-email-1.2.jar

What am I doing wrong?

Thanks.

解决方案

Not sure why SimpleEmail didn't work. But this did:

import javax.mail.*;
  import javax.mail.internet.*;

  import java.util.Properties;

  public class Email {

     public static void sendMessage(String emailaddress, String subject, String body) {
        try {
           Properties props = new Properties();
           props.setProperty("mail.transport.protocol", "smtp");
           props.setProperty("mail.host", "myhost");

           Session mailSession = Session.getDefaultInstance(props, null);
           Transport transport = mailSession.getTransport();

           MimeMessage message = new MimeMessage(mailSession);
           message.setSubject("Testing javamail plain");
           message.setContent("This is a test", "text/plain");
           message.addRecipient(Message.RecipientType.TO, new InternetAddress("test@example.com"));

           transport.connect();
           transport.sendMessage(message,
                   message.getRecipients(Message.RecipientType.TO));
           transport.close();
        } catch (Exception ex) {
           ex.printStackTrace();
        }
     }
  }

Thanks for the suggestions.

这篇关于当我在Java中实例化一个SimpleEmail类时,为什么会收到这个异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆