Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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 来自URL的Apache多端口邮件需要cookie_Java_Spring_Cookies_Excel - Fatal编程技术网

Java 来自URL的Apache多端口邮件需要cookie

Java 来自URL的Apache多端口邮件需要cookie,java,spring,cookies,excel,Java,Spring,Cookies,Excel,我需要通过HTTP获取excel电子表格,然后将其作为附件发送到JavaSpring服务器上的电子邮件中 我发现的问题是MultiPartEmail.attach()只接受一个java.net.URL实例,我无法确定如何确保请求头中有特定的Cookie进行身份验证 url = new URL(urlString); email.attach(url, "test.xls", "File"); email.send(); 我曾尝试手动请求并创建工作簿,但后来我在将工作簿本身附加到Multipar

我需要通过HTTP获取excel电子表格,然后将其作为附件发送到JavaSpring服务器上的电子邮件中

我发现的问题是MultiPartEmail.attach()只接受一个java.net.URL实例,我无法确定如何确保请求头中有特定的Cookie进行身份验证

url = new URL(urlString);
email.attach(url, "test.xls", "File");
email.send();
我曾尝试手动请求并创建工作簿,但后来我在将工作簿本身附加到MultipartMail上时遇到了难题

HttpClient client = new HttpClient();
GetMethod method = new GetMethod(queryString);

method.setRequestHeader("Cookie", cookie);
client.executeMethod(method);

InputStream stream = method.getResponseBodyAsStream();

Workbook workbook = Workbook.getWorkbook(stream);
email.attach(workbook, "report.xls", "forecasting report");
我需要一些方法来克服这些限制


提前感谢您的时间。

由于您使用的是Spring,您可以使用其内置的电子邮件支持发送电子邮件。因此,如何从磁盘或其他地方检索文件并不重要。您可以使用MIMessageHelper发送带有附件的电子邮件,并在邮件主机上指定帐户的用户名和密码,以便进行如下身份验证:

public class EmailNotifier {
private JavaMailSenderImpl mailSender;

public void setMailSender(JavaMailSenderImpl mailSender) {
    this.mailSender = mailSender;
}

public void sendMail(InternetAddress fromAddress, InternetAddress toAddress, String subject, String msg) {

    MimeMessage message = mailSender.createMimeMessage();

    try {
        // use the true flag to indicate you need a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(fromAddress);
        helper.setTo(toAddress);
        helper.setSubject(subject);
        helper.setText(msg);
        // let's attach the infamous windows Sample file (this time copied to c:/)
        FileSystemResource file = new FileSystemResource(new File("c:/test.xls"));
        helper.addAttachment("test.xls", file);
        mailSender.send(message);
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }           
}
}
并在bean配置文件中配置JavaMailSenderImpl bean。这是通过Gmail发送电子邮件

beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="587" />
        <property name="username" value="adminxxxx" />
        <property name="password" value="password" />

        <property name="javaMailProperties">
           <props>
                  <prop key="mail.smtp.auth">true</prop>
                  <prop key="mail.smtp.starttls.enable">true</prop>
               </props>
        </property>
    </bean>

    <bean id="emailNotifier" class="com.examples.EmailNotifier">
        <property name="mailSender" ref="mailSender" />
    </bean>

</beans>

由于您使用的是Spring,您可以使用其内置的电子邮件支持发送电子邮件。因此,如何从磁盘或其他地方检索文件并不重要。您可以使用MIMessageHelper发送带有附件的电子邮件,并在邮件主机上指定帐户的用户名和密码,以便进行如下身份验证:

public class EmailNotifier {
private JavaMailSenderImpl mailSender;

public void setMailSender(JavaMailSenderImpl mailSender) {
    this.mailSender = mailSender;
}

public void sendMail(InternetAddress fromAddress, InternetAddress toAddress, String subject, String msg) {

    MimeMessage message = mailSender.createMimeMessage();

    try {
        // use the true flag to indicate you need a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(fromAddress);
        helper.setTo(toAddress);
        helper.setSubject(subject);
        helper.setText(msg);
        // let's attach the infamous windows Sample file (this time copied to c:/)
        FileSystemResource file = new FileSystemResource(new File("c:/test.xls"));
        helper.addAttachment("test.xls", file);
        mailSender.send(message);
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }           
}
}
并在bean配置文件中配置JavaMailSenderImpl bean。这是通过Gmail发送电子邮件

beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="587" />
        <property name="username" value="adminxxxx" />
        <property name="password" value="password" />

        <property name="javaMailProperties">
           <props>
                  <prop key="mail.smtp.auth">true</prop>
                  <prop key="mail.smtp.starttls.enable">true</prop>
               </props>
        </property>
    </bean>

    <bean id="emailNotifier" class="com.examples.EmailNotifier">
        <property name="mailSender" ref="mailSender" />
    </bean>

</beans>

谢谢,这真的很有帮助,但遥控xls仍有问题。我想我现在的问题是如何从inputstream创建本地.xls,或者从创建xls文件的url创建更低级的.xls。您使用什么容器部署应用程序?已修复!最后,我只是将原始字节放入一个临时文件,通过电子邮件发送,然后删除。谢谢,这真的很有帮助,但我仍然有远程xls的问题。我想我现在的问题是如何从inputstream创建本地.xls,或者从创建xls文件的url创建更低级的.xls。您使用什么容器部署应用程序?已修复!我最终只是将原始字节放入一个临时文件,通过电子邮件发送,然后删除它。