Java Email API电子邮件未正确发送

时间:2013-05-24 12:39:18

标签: java email

我正在使用Java电子邮件API来发送电子邮件功能。它似乎没有用,因为没有收到任何电子邮件。我的代码中没有任何异常(所以它似乎正确连接到smtp),当我调试它时,似乎没问题。所以我想知道我的代码是否还有其他问题?这里也是相关的进口产品。还有一个" createBody()"在类中的方法,但它只用信息填充消息,所以我认为不感兴趣。

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class Mail {

    public Mail() {
    }

    static public boolean sendHtml(Commission commission, String mailServer, InternetAddress fromAddress, InternetAddress ccAddress, String subject, String header) throws Exception {

        try {
            String body = createBody(commission);
            InternetAddress toAddress = new InternetAddress(commission.getForm().getActiveReceiver().getEmail());

            Properties props = new Properties();
            props.put("mail.smtp.host", mailServer);
            Session session = Session.getDefaultInstance(props);

            //Instantiate a message holder
            Message msg = new MimeMessage(session);
            msg.setHeader("Content-Type", "text/plain; charset='iso-8859-1'");

            //From
            msg.setFrom(fromAddress);

            //To
            msg.addRecipient(Message.RecipientType.TO, toAddress);

            //CC
            if (ccAddress != null)
                msg.addRecipient(Message.RecipientType.CC, ccAddress);

            //Subject
            if (!subject.equals(""))
                msg.setSubject(subject);

            //Date
            msg.setSentDate(new Date());

            //Begin
            StringBuffer sb = new StringBuffer();
            sb.append("<html>\n");
            sb.append("<head>\n");
            sb.append("<title>\n");
            sb.append("</title>\n");
            sb.append("<style type=\"text/css\">\n");
            sb.append("*{font-family: Tahoma; font-size: 11px; font-weight: normal; color: #51514F;}\n");
            sb.append("body{background-color: #FEF8EA;}\n");
            sb.append("tr.odd {background-color: #F3F3F3;border: 1px #F3F3F3 solid;}\n");
            sb.append("tr.even {background-color: #FFFFFF;border: 1px #FFFFFF solid;}\n");
            sb.append("th {font-weight: bold; background-color:#F3F3F3; border-bottom: 1px solid #E2D6BC;}\n");
            sb.append("p.header {font-weight: bold;}\n");
            sb.append("table {border-collapse:collapse; border: 1px solid #E2D6BC;}\n");
            sb.append("td.center {text-align:center;}\n");
            sb.append("</style>\n");
            sb.append("</head>\n");
            sb.append("<body>\n");

            //Header
            if (!header.equals(""))
                sb.append("<h1>"+header+"</h1>");

            //Body
            if (!body.equals("")) 
                sb.append(body);

            //End
            sb.append("</body>\n");
            sb.append("</htmnl>\n");

            msg.setContent(sb.toString(), "text/html");
            System.out.println("About to connect");
            //Connect
            Transport transport = session.getTransport("smtp");
            transport.connect();


            //Sending the mail
    transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));

            //Sending the CC mail
            if (ccAddress != null)
        transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.CC));

            //Close
            transport.close();

        } catch (Exception e) {
            throw e;
        }
        return true;
    }

}

2 个答案:

答案 0 :(得分:0)

尝试以下方法。它可能对你有帮助。

 Session mailSession = Session.getInstance(System.getProperties());    
 Transport transport 
      = new SMTPTransport(mailSession,new URLName("mail.your-server.com"));    
 transport.connect("mail.your-server.com",portNumber,userName,userPassword);

〔实施例:

 Session mailSession = Session.getInstance(System.getProperties());
 Transport transport 
     = new SMTPTransport(mailSession,new URLName("mail.prabhakar.com"));
 transport.connect("mail.prabhakar.com",25,"prabhakar","******");

答案 1 :(得分:0)

您可以启用smtp调试,查看SMTP级别发生的情况:

Properties props = new Properties();
props.put("mail.smtp.host","<MAILHOST>");
props.put("mail.debug", true);
Session session = Session.getInstance(props);