Not difficult, please see JavaMail send the attachment example

Source: Internet
Author: User
Tags mail
Here are the code to send an attachment:

Import java.util.Properties;
Import javax.mail.*;
Import javax.mail.internet.*;
Import javax.activation.*;

public class Attachexample {
public static void Main (String args[])
Throws Exception {
String host = Args[0];
String from = args[1];
String to = args[2];
String fileattachment = args[3];

Get System Properties
Properties props = System.getproperties ();

Setup Mail Server
Props.put ("Mail.smtp.host", host);

Get sessions
Session session =
Session.getinstance (props, null);

Define message
MimeMessage message =
New MimeMessage (session);
Message.setfrom (
New InternetAddress (from));
Message.addrecipient (
Message.RecipientType.TO,
New InternetAddress (to));
Message.setsubject (
"Hello JavaMail attachment");

Create the message part
MimeBodyPart Messagebodypart =
New MimeBodyPart ();

Fill Message
Messagebodypart.settext ("Hi");

Multipart Multipart = new Mimemultipart ();
Multipart.addbodypart (Messagebodypart);

Part Two is attachment
Messagebodypart = new MimeBodyPart ();
DataSource Source =
New Filedatasource (fileattachment);
Messagebodypart.setdatahandler (
New DataHandler (source));
Messagebodypart.setfilename (fileattachment);
Multipart.addbodypart (Messagebodypart);

Put parts in
Message.setcontent (multipart);

Send the message
Transport.send (message);
}
}



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.