使用Java发送电子邮件

时间:2009-11-23 17:31:30

标签: java email servlets

 try{
        Properties props = new Properties();
        props.put("mail.smtp.host", "ipc-smtp.bits-pilani.ac.in");
        Session sess = Session.getInstance(props, null);
        sess.setDebug(true);
        Message msg = new MimeMessage(sess);
        InternetAddress addressFrom = new InternetAddress("mymail@gmail.com");
        msg.setFrom(addressFrom);
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress("mymail@gmail.com"));
        msg.addHeader("MyHeaderName", "myHeaderValue");
        msg.setSubject("Test");
        msg.setContent("Yippe", "text/plain");
        Transport.send(msg);
        }catch(Exception exp){
           exp.printStackTrace();
        }

错误是javax.mail.MessagingException:554由于zen-spamhaus RBL操作阻止了邮件

这是我学院的smtp服务器。

2 个答案:

答案 0 :(得分:1)

我会通知你大学的IT部门,他们应该能够处理这个问题。虽然看起来他们留下了一个开放的接力,但也许不是。

答案 1 :(得分:0)

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

.....

public static void postMail(String[] recipients, String subject, String message, String from) throws MessagingException {
    Properties props = new Properties();
    props.put("mail.smtp.host", Util.getProperty("smtpHost"));
    Session session = Session.getDefaultInstance(props, null);
    Message msg = new MimeMessage(session);
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);
    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    //msg.addHeader("MyHeaderName", "myHeaderValue");
    msg.setSubject(subject);
    msg.setContent(message, "text/html");
    Transport.send(msg);
}