我创建了一个用于发送邮件的java程序,我导入了一些与该程序相关的包。我已下载软件包 javax.mail 和 javax.activation ,并将其放入 C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \分机
我编译我的程序然后它编译但是当我运行它时会抛出异常。
我无法理解它为什么会抛出异常。
我的代码在这里。
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.net.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "rs.89.rj@gmail.com";
// Sender's email ID needs to be mentioned
String from = "manoj1990gupta@yahoo.in";
try{
// Assuming you are sending email from localhost
String host = InetAddress.getLocalHost().toString();
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}}catch(Exception e){}
}
}
答案 0 :(得分:1)
您需要下载JavaMail API包并将其放入类路径中,但请确保包含mail.jar以及可以在lib文件夹中找到的所有依赖库。
从命令行设置类路径:
java -cp "mail.jar;lib/*" SendEMail