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