Send an email with an attachment with JavaMail

Source: Internet
Author: User
Tags character set

With the continuous promotion of network applications, e-mail is more and more used by everyone. Although we often associate e-mail with e-mail clients such as Foxmail and Outlook, we often need to do our own programming to send incoming messages, such as receipt mail from a website after a registered user, or when shopping on the internet, Send a confirmation email within a few minutes after the order is completed. For such a requirement, we cannot use the existing mail client and need to write our own mail delivery or handler. Here to explain how to use JavaMail to achieve mail delivery.

Note: This article is only intended to discuss some of the tips for JavaMail to send and receive mail with attachments, so just give some code.

1. Send a message with an attachment

We usually send the message can be divided into 2 major parts, one is the sender, the letter, the subject and other mail headers, the other part is the content of the message, it includes the attachment of the message. When we send an ordinary message the type of content set is "text/html", with the attachment, we need to set the content type to multipart, when the content includes the attachment and "text/html" type of body. Here's how to put the attachment in the mail.

Private Multipart Getmultipart () throws Messagingexception,unsupportedencodingexception
{
Mimemultipart MP = new Mimemultipart ();   
Try
{
//Set contents in content
MimeBodyPart CONTENTMBP = new MimeBodyPart ();
Please specify the character set, otherwise it will be garbled
Contentmbp.setcontent (_mailcontent.getcontent (), text/html; charset=gb2312 ");
Mp.addbodypart (CONTENTMBP);//Add Attachments
for (int i=0;i<_mailattachment.getattachpath (). Size (); i++)
{ MimeBodyPart MBP = new MimeBodyPart ();
Filedatasource FDS = new Filedatasource ((String) _mailattachment.getattachpath (). get (i));
Mbp.setdatahandler (New DataHandler (FDS));
Mbp.setfilename (Mimeutility.encodeword (Fds.getname (), "GB2312", null));
Mp.addbodypart (MBP);
}
}
catch (Messagingexception IE)
{
System.out.println ("Set Content message Error ..." +ie.g Etmessage ());
Throw ie;
}
catch (Unsupportedencodingexception IE)
{
SysteM.out.println ("Encode the FileName error ..." +ie.getmessage ());
Throw ie;
}
return MP;
}

The following considerations for placing attachments are:

You need to be aware of the character set when you send mail. Not only content to be set, and file name also need to set. If we remove Mbp.setfilename (Mimeutility.encodeword (Fds.getname (), "GB2312", null), then the attachment you selected will be brought to the email but not seen in the attachment. We can know by looking at the message size. We use this feature to deliver messages that are written in the HTML language, and that contain picture information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.