我创建了一个用于发送邮件的 java 程序,并导入了一些与该程序相关的包。我已下载包javax.mail和javax.activation并将其放入C:\Program Files\Java\jdk1.7.0\jre\lib\ext
我编译我的程序然后它编译但是当我运行它时抛出异常。
我无法理解为什么它会抛出异常。
我的代码在这里。
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){}
}
}