Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

jquery - Sending email as attachment in java

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...