【问题标题】:Apache MultiPartEmail from URL requiring cookie来自需要 cookie 的 URL 的 Apache MultiPartEmail
【发布时间】:2012-08-01 13:48:52
【问题描述】:

我需要通过 HTTP 获取一个 Excel 电子表格,然后将其作为附件发送到我的 Java Spring 服务器上的电子邮件中。

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

url = new URL(urlString);
email.attach(url, "test.xls", "File");
email.send();

我试图手动请求并创建一个工作簿,但后来我很难将工作簿本身附加到 MultiPartEmail。

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");

我需要一些方法来解决这些限制。

提前感谢您的宝贵时间。

【问题讨论】:

    标签: java spring cookies excel


    【解决方案1】:

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

    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>
    

    这只是为了测试:

    public class MailApp {
      public static void main( String[] args )
        {
            ApplicationContext context = 
                 new ClassPathXmlApplicationContext("beans.xml");
    
            EmailNotifier emailNotifier = (EmailNotifier) context.getBean("emailNotifier");
            try {
                emailNotifier.sendMail(new InternetAddress("adminxxxx@gmail.com"),
                        new InternetAddress("recipientxxxx@gmail.com"),
                       "Email Alert!", 
                       "Hello User, This is a test email \n No response required.");
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
    

    Spring Documentation

    【讨论】:

    • 谢谢,这真的很有帮助,但我仍然无法使用远程 xls。我想我现在的问题是如何从输入流或更低级别从创建 xls 文件的 url 创建本地 .xls。
    • 您使用什么容器来部署您的应用程序?
    • 已修复!我最终只是将原始字节打包到一个临时文件中,通过电子邮件发送它,然后将其删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2013-05-27
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多