在java中作为附件发送电子邮件 [英] Sending email as attachment in java

查看:105
本文介绍了在java中作为附件发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在d盘中有一张图片,我想将它作为电子邮件附件发送给 java 。收件人邮件将由发件人输入,我只想将其附加到我的电子邮件帐户。请帮忙。
谢谢。

解决方案

检查电子邮件发送实用程序代码的链接(我对该问题的回答)。您必须添加几行代码才能发送带有附件的邮件。



关于提交信息应该发到邮件

在EmailUtility.java之后

  msg.setSentDate(new Date()); 

评论

  msg.setText(消息); 




并添加以下代码:

  //创建消息部分
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message,text / html);
String attachFile =C:/imgname.jpg;
//创建多部分
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//添加附件

if(reason.equals(attach))
if(attachFile!= null){
MimeBodyPart attachPart = new MimeBodyPart() ;
attachPart.attachFile(attachFile);
multipart.addBodyPart(attachPart);
}

//将多部分设置为电子邮件的内容
msg.setContent(multipart);

您必须将C:/imgname.jpg更改为您的文件名及其路径。

I have an image in my d drive and i want to send it as an email attachment in java. Recipients mail will be entered by sender, I just want to attach it to my email account. Please help. thank you.

解决方案

Check out the link(my answer to that question) for email sending utility code. You've got to add few line of code to send mail with attachment.

On submit the information should come to email

In EmailUtility.java after

msg.setSentDate(new Date());

comment

msg.setText(message); 

And add the following code:

     // creates message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(message, "text/html");
    String attachFile = "C:/imgname.jpg";
    // creates multi-part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    // adds attachments

    if(reason.equals("attach"))
    if (attachFile != null) {
            MimeBodyPart attachPart = new MimeBodyPart();
            attachPart.attachFile(attachFile);
            multipart.addBodyPart(attachPart);
    }

    // sets the multi-part as e-mail's content
    msg.setContent(multipart);

You've got to change C:/imgname.jpg to your file name along with its path.

这篇关于在java中作为附件发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆