Java programs send inline images, email with attachments

Source: Internet
Author: User

Package mail;

Import java.io.UnsupportedEncodingException;
Import java.util.Properties;

Import Javax.activation.DataHandler;
Import Javax.activation.FileDataSource;
Import Javax.mail.Authenticator;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;
Import javax.mail.internet.MimeUtility;

public class SendMail2 {

/**
* @param args
* @throws messagingexception
* @throws addressexception
* @throws unsupportedencodingexception
*/
public static void Main (string[] args) throws Addressexception, Messagingexception, unsupportedencodingexception {
Properties Props = new properties ();//environment variable settings.
Props.setproperty ("Mail.transport.protocol", "SMTP");//protocol used for sending
Props.setproperty ("Mail.host", "smtp.163.com");//host address of the sending server
Props.setproperty ("Mail.smtp.auth", "true");//Request Authentication
Session session = Session.getdefaultinstance (Props,new Authenticator () {
@Override
Protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication ("[Email protected]", "you guess?" ");
}
});
MimeMessage message = new MimeMessage (session);//Represents an email
Message.setfrom (New InternetAddress ("[email protected]");//Set Sender
Message.setrecipients (Message.RecipientType.TO, "[email protected]");//Set Recipient
Message.setsubject ("This is a message with images embedded in text");//Set theme
MimeBodyPart textpart=new MimeBodyPart ();
Textpart.setcontent ("Beauty bbbbbb", "text/html;charset=utf-8");
Inline picture section
MimeBodyPart Imagepart = new MimeBodyPart ();
DataHandler dh = new DataHandler (New Filedatasource ("c:/1.jpg"));
Imagepart.setdatahandler (DH);
Imagepart.setcontentid ("mm");
Text and inline picture fit
Mimemultipart mpart1 = new Mimemultipart ();
Mpart1.addbodypart (Textpart);
Mpart1.addbodypart (Imagepart);
Mpart1.setsubtype ("related");
MimeBodyPart Textimagepart = new MimeBodyPart ();
Textimagepart.setcontent (MPART1);

MimeBodyPart Attachmentpart = new MimeBodyPart ();
DH = new DataHandler (New Filedatasource ("c:/attachment. zip"));
String filename = dh.getname ();//Get file name
SYSTEM.OUT.PRINTLN (filename);
Attachmentpart.setdatahandler (DH);
Attachmentpart.setfilename (Mimeutility.encodetext (filename));//Manually set the file name. Note The encoding for the Chinese file name


Mimemultipart mpart2 = new Mimemultipart ();
Mpart2.addbodypart (Textimagepart);//text plus inline image
Mpart2.addbodypart (Attachmentpart);//Accessories
Mpart2.setsubtype ("mixed");//complex relationship
Message.setcontent (MPART2);

Send mail
Transport ts = Session.gettransport ();
Ts.connect ("[Email protected]", "Do you guess?" ");//Connection
Ts.sendmessage (Message, message.getallrecipients ());
Ts.close ();
Transport.send (message);
}

}

Java programs send inline images, email with attachments

Related Article

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.