java邮件程序被阻止了

时间:2014-12-11 14:11:19

标签: java email

我正在尝试以下程序。

=============================================== ============================

package com.learn.java.networking;


import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

public class MailSenderSmtpLinux

{
// reportFileName = TestExecutionResultFileName
public static void execute(String reportFileName) throws Exception{

    String path = "C:\\Users\\polac\\Pictures\\";

    String[] to = { "chandra.pola@emc.com", "chandraspola@hotmail.com",
            "chandrasekhar.pola@gmail.com" };
    String[] cc = {};
    String[] bcc = {};

    MailSenderSmtpLinux.sendMail("root@RHEL6167.avuiblr.com", "8RttoTriz",
            "10.31.183.167", "465", "true", "true", true,
            "javax.net.ssl.SSLSocketFactory", "false", to, cc, bcc,
            "testimagemail", "Hai this is my test report", path,
            reportFileName);
}

public static boolean sendMail(String userName, String passWord,
        String host, String port, String starttls, String auth,
        boolean debug, String socketFactoryClass, String fallback,
        String[] to, String[] cc, String[] bcc, String subject,
        String text, String attachmentPath, String attachmentName) {

    // Object Instantiation of a properties file.
    Properties props = new Properties();

    props.put("mail.smtp.user", userName);

    props.put("mail.smtp.host", host);

    if (!"".equals(port)) {
        props.put("mail.smtp.port", (String) port);
    }

    if (!"".equals(starttls)) {
        props.put("mail.smtp.starttls.enable", starttls);
        props.put("mail.smtp.auth", auth);
    }

    if (debug) {

        props.put("mail.smtp.debug", "true");

    } else {

        props.put("mail.smtp.debug", "false");

    }

    if (!"".equals(port)) {
        props.put("mail.smtp.socketFactory.port", port);
    }
    if (!"".equals(socketFactoryClass)) {
        props.put("mail.smtp.socketFactory.class", socketFactoryClass);
    }
    if (!"".equals(fallback)) {
        props.put("mail.smtp.socketFactory.fallback", fallback);
    }

    try {

        Session session = Session.getDefaultInstance(props, null);

        session.setDebug(debug);

        MimeMessage msg = new MimeMessage(session);

        msg.setText(text);

        msg.setSubject(subject);

        Multipart multipart = new MimeMultipart();
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(attachmentPath
                + attachmentName);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(attachmentName);
        // messageBodyPart.setText(text);

        multipart.addBodyPart(messageBodyPart);

        msg.setContent(multipart);
        msg.setFrom(new InternetAddress(userName));

        for (int i = 0; i < to.length; i++) {
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    to[i]));
        }

        for (int i = 0; i < cc.length; i++) {
            msg.addRecipient(Message.RecipientType.CC, new InternetAddress(
                    cc[i]));
        }

        for (int i = 0; i < bcc.length; i++) {
            msg.addRecipient(Message.RecipientType.BCC,
                    new InternetAddress(bcc[i]));
        }

        msg.saveChanges();

        Transport transport = session.getTransport("smtp");

        transport.connect(host, Integer.parseInt(port), userName, passWord);

        transport.sendMessage(msg, msg.getAllRecipients());

        transport.close();

        return true;

    } catch (Exception mex) {
        mex.printStackTrace();
        return false;
    }
}

public static void main(String args[]) {
    String ExecutionFileName = "gajendramoksham.jpg";
    try {
        MailSenderSmtpLinux.execute(ExecutionFileName);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

=============================================== ============================

程序的输出只是告诉我这一点,而不是抛出任何例外。

DEBUG:setDebug:JavaMail 1.5.2版

DEBUG:getProvider()返回javax.mail.Provider [TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]

DEBUG SMTP:useEhlo是true,useAuth false

DEBUG SMTP:尝试连接到主机&#34; 10.31.183.167&#34;,端口465,isSSL true

该程序使用mail.jar和javax.mail.jar编译,在这两种情况下都是相同的。

是否有任何身体遇到同样的问题?任何人都可以帮我解决问题。

提前致谢

- 钱德拉

0 个答案:

没有答案