Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java邮件程序被阻止_Java_Email - Fatal编程技术网

java邮件程序被阻止

java邮件程序被阻止,java,email,Java,Email,我正在尝试以下程序 =========================================================================== package com.learn.java.networking; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import java

我正在尝试以下程序

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

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();
    }
}
}
package com.learn.java.networking;
导入javax.activation.DataHandler;
导入javax.activation.DataSource;
导入javax.activation.FileDataSource;
导入javax.mail.*;
导入javax.mail.internet.*;
导入java.util.*;
公共类MailSenderSmtpLinux
{
//reportFileName=TestExecutionResultFileName
公共静态void execute(String reportFileName)引发异常{
String path=“C:\\Users\\polac\\Pictures\\”;
字符串[]到={“chandra。pola@emc.com", "chandraspola@hotmail.com",
“钱德拉塞卡。pola@gmail.com" };
字符串[]cc={};
字符串[]bcc={};
MailSenderSmtpLinux.sendMail(“root@RHEL6167.avuiblr.com“,“8RttoTriz”,
“10.31.183.167”,“465”,“真”,“真”,“真”,真,
“javax.net.ssl.SSLSocketFactory”,“false”,收件人,抄送,密件抄送,
“testimagemail”,“这是我的测试报告”,路径,
报告文件名);
}
公共静态布尔sendMail(字符串用户名、字符串密码、,
字符串主机、字符串端口、字符串starttls、字符串身份验证、,
布尔调试、字符串socketFactoryClass、字符串回退、,
字符串[]收件人,字符串[]抄送,字符串[]密件抄送,字符串主题,
字符串文本、字符串附件路径、字符串附件名称){
//属性文件的对象实例化。
Properties props=新属性();
props.put(“mail.smtp.user”,用户名);
props.put(“mail.smtp.host”,host);
如果(!“”.equals(端口)){
put(“mail.smtp.port”,(字符串)端口);
}
如果(!“”.equals(starttls)){
props.put(“mail.smtp.starttls.enable”,starttls);
put(“mail.smtp.auth”,auth);
}
如果(调试){
props.put(“mail.smtp.debug”,“true”);
}否则{
props.put(“mail.smtp.debug”,“false”);
}
如果(!“”.equals(端口)){
props.put(“mail.smtp.socketFactory.port”,port);
}
如果(!“”.equals(socketFactoryClass)){
props.put(“mail.smtp.socketFactory.class”,socketFactoryClass);
}
如果(!“”.equals(回退)){
props.put(“mail.smtp.socketFactory.fallback”,fallback);
}
试一试{
Session Session=Session.getDefaultInstance(props,null);
setDebug(debug);
MimeMessage msg=新MimeMessage(会话);
msg.setText(文本);
msg.setSubject(主题);
Multipart Multipart=新的MimeMultipart();
MimeBodyPart messageBodyPart=新的MimeBodyPart();
DataSource source=新文件DataSource(attachmentPath
+附件名称);
setDataHandler(新的DataHandler(源));
messageBodyPart.setFileName(attachmentName);
//messageBodyPart.setText(文本);
multipart.addBodyPart(messageBodyPart);
msg.setContent(多部分);
msg.setFrom(新的Internet地址(用户名));
for(int i=0;i
===========================================================================

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();
    }
}
}
程序的输出只是简单地向我展示了这一点,而没有抛出任何异常

调试:setDebug:JavaMail版本1.5.2

调试:getProvider()返回javax.mail.Provider[传输,smtps,com.sun.mail.smtp.SMTPSSLTTransport,Oracle]

调试SMTP:useEhlo true,useAuth false

调试SMTP:正在尝试连接到主机“10.31.183.167”,端口465,isSSL true

程序是用mail.jar和javax.mail.jar编译的,在这两种情况下问题是相同的

有没有人面临同样的问题?有人能帮我解决这个问题吗

提前谢谢


--Chandra

您好,这取决于服务器配置,是否允许外部应用程序连接到邮件服务器。您可能需要联系mailServer管理员,询问他们电子邮件服务器设置,并告诉他们允许为您的帐户使用外部应用程序。另外,请发布错误日志,为什么粘贴调试日志?阅读标题后,我立即想到,邮件不在邮件服务器上。所以
RHEL6167.avuiblr.com
不是
10.31.183.167
。那么许多收件人将不会收到“垃圾邮件”。谢谢阿克谢和艾根的回复。我正在尝试使用运行smtp的邮件服务器从Java代码向外部世界发送邮件。RHEL6167.avuiblr.com和10.31.183.167相同。上面提到的邮件服务器是由我设置的,所以我是所谓机器的管理员。谢谢Akshay和Eggen的回复。我正在尝试使用运行smtp的邮件服务器从Java代码向外部世界发送邮件。RHEL6167.avuiblr.com和10.31.183.167相同。上面提到的邮件服务器是由我设置的,所以我是所谓机器的管理员。我粘贴的日志是ru上包含的日志