我正在尝试制作一个非常简单的电子邮件应用程序,并且我已经编写了几行基本代码。我不断得到的一个例外是com.sun.mail.util.MailConnectException
. 有没有一种简单的方法可以通过代理或防火墙对我的方式进行编码,而不会弄乱发送机器的连接设置?
到目前为止我的代码:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendHTMLMail {
public static void main(String[] args) {
// Recipient ID needs to be set
String to = "test@test.com";
// Senders ID needs to be set
String from = "mytest@test.com";
// Assuming localhost
String host = "localhost";
// System properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
//Get default session object
Session session = Session.getDefaultInstance(properties);
try {
// Default MimeMessage object
MimeMessage mMessage = new MimeMessage(session);
// Set from
mMessage.setFrom(new InternetAddress(from));
// Set to
mMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set subject
mMessage.setSubject("This is the subject line");
// Set the actual message
mMessage.setContent("<h1>This is the actual message</h1>", "text/html");
// SEND MESSAGE
Transport.send(mMessage);
System.out.println("Message sent...");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}