Message attachment garbled, set the message reply person, set the sender recipient name, send the existing EML file

Source: Internet
Author: User

It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
Troubleshoot message attachment garbled problem

Associate a data source for attachment 1
	   DataSource DS1 = new Filedatasource ("C:\\Documents and settings\\administrator\\ Desktop \\river.jpg");
	   DataHandler DH1 = new DataHandler (DS1);
	   Attch1.setdatahandler (DH1);
	   The file name must be encoded, otherwise the attachment name will be garbled
	   Attch1.setfilename (Mimeutility.encodetext ("hometown of river. jpg"));

set up a mail reply person

      Set Sender
       Msg.setfrom (New InternetAddress (Mimeutility.encodetext ("Xiao Hua") + "<xh216319@163.com>");
       Set the respondent
       Msg.setreplyto (Internetaddress.parse ("itstar1965@sina.com"));
       Set the recipient and display the Msg.setrecipients (
       recipienttype.to,internetaddress.parse (
    	 mimeutility.encodetext (") with a friendly name ITSTAR ") +" <ITSTAR1965@SINA.COM>, "+mimeutility.encodetext (" Xiao Hua ") +" <xh216319@163.com> ")
    	;

Set Mail Sender recipient name

      Set Sender
       Msg.setfrom (New InternetAddress (Mimeutility.encodetext ("Xiao Hua") + "<xh216319@163.com>");
       Set the respondent
       Msg.setreplyto (Internetaddress.parse ("itstar1965@sina.com"));
       Set the recipient and display the Msg.setrecipients (
       recipienttype.to,internetaddress.parse (
    	 mimeutility.encodetext (") with a friendly name ITSTAR ") +" <ITSTAR1965@SINA.COM>, "+mimeutility.encodetext (" Xiao Hua ") +" <xh216319@163.com> ")
    	;

Complete procedure for solving the above problems

Package edu.mail.util;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import java.util.Properties;
Import Javax.activation.DataHandler;
Import Javax.activation.DataSource;
Import Javax.activation.FileDataSource;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Message.RecipientType;
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 SendMultipartMessageDemo3 {public static void main (string[] args) throws Messagingexception, IOException {
	   Properties Props = new properties ();
	   Session session = Session.getinstance (props);
       Session.setdebug (TRUE);
       Message msg = new MimeMessage (session);
       Msg.setsubject ("The first multipart javaMail"); Set Sender Msg.setfrom (New internetaddress (MimeUtility.encodetext ("Xiao Hua") + "<xh216319@163.com>");
       Set the respondent Msg.setreplyto (Internetaddress.parse ("itstar1965@sina.com")); Set the recipient and display Msg.setrecipients (Recipienttype.to,internetaddress.parse (Mimeutility.encodetext ("ITSTAR") + "with a friendly name
       
	   <itstar1965@sina.com&gt, "+mimeutility.encodetext (" Xiao Hua ") +" <xh216319@163.com> ");
	   Mimemultipart Msgmultipart = new Mimemultipart ("mixed");
	   Msg.setcontent (Msgmultipart);
	   Create a content component and Attch component for the message object MimeBodyPart content = new MimeBodyPart ();
	   MimeBodyPart attch1 = new MimeBodyPart ();
       MimeBodyPart attch2 = new MimeBodyPart ();
	   Add two Attachment objects and body objects to the Msgmultipart object Msgmultipart.addbodypart (content);
	   Msgmultipart.addbodypart (ATTCH1);
	   
	   Msgmultipart.addbodypart (ATTCH2);
	   Associate a data source for attachment 1 DataSource ds1 = new Filedatasource ("C:\\Documents and settings\\administrator\\ Desktop \\river.jpg");
	   DataHandler DH1 = new DataHandler (DS1);
	   Attch1.setdatahandler (DH1); TextThe name must be encoded, otherwise the attachment name will be garbled Attch1.setfilename (Mimeutility.encodetext ("Hometown of the river. jpg"));
	   Associate a data source for attachment 2 DataSource ds2 = new Filedatasource ("C:\\Documents and settings\\administrator\\ Desktop \\mail.bat");
	   DataHandler DH2 = new DataHandler (DS2);
	   Attch2.setdatahandler (DH2);
	   Attch2.setfilename ("Mail.bat"); Because the body content is also a multipart object, its components are related relationships, so content.setcontent (Contentmultipart) Mimemultipart Contentmultipart
	   = new Mimemultipart ("related");
	   Content.setcontent (Contentmultipart);
	   Create Htmlpart components and Gifpart components for contentmultipart mimebodypart Htmlpart = new MimeBodyPart ();
	   MimeBodyPart Gifpart = new MimeBodyPart ();
	   Add the Htmlpart and Gifpart objects to the Contentmultipart object Contentmultipart.addbodypart (Htmlpart);
	   
	   Contentmultipart.addbodypart (Gifpart);
	   DataSource DS3 = new Filedatasource ("C:\\Documents and settings\\administrator\\ Desktop \\2012_03130103.JPG");
	   DataHandler dh3 = new DataHandler (DS3);
	   Gifpart.setdatahandler (DH3); Gifpart.setfilename ("Itstar.GIF ");
	   Gifpart.setheader ("Content-location", "http://www.csdn.net/itstar.gif"); Here src= "Http://www.csdn.net/itstar.gif" is actually pointing to its associated Gifpart object Htmlpart.setcontent ("This is my first composite structure of javamail, my photo <
	   IMG height=100px,width=80px src= ' http://www.csdn.net/itstar.gif '/> "," text/html;charset=utf-8 ");
	   Save the changes made by the message msg.savechanges (); OutputStream Ops = new FileOutputStream ("C:\\Documents and settings\\administrator\\ desktop \ \
         Sendmultipartmessagedemo3.eml ");
	   Writes a message object to the file stream Msg.writeto (OPS);
	Ops.close ();
 }

}

to send an existing EML file

Package edu.mail.util;
Import Java.io.FileInputStream;
Import Java.util.Date;

Import java.util.Properties;
Import Javax.mail.Authenticator;
Import Javax.mail.Message;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import Javax.mail.Message.RecipientType;
Import javax.mail.internet.InternetAddress;

Import Javax.mail.internet.MimeMessage; public class SendEmlFileDemo4 {public static void main (string[] args) throws Exception {//Create Session object containing network connection information for mail server
			   。
			   Properties Props = new properties ();
			   Props.setproperty ("Mail.transport.protocol", "SMTP");
			   Props.setproperty ("Mail.host", "smtp.163.com");
			   Props.setproperty ("Mail.smtp.auth", "true"); The difference between the/** * getinstance () and the Getdefaultinstance () method: * getinstance () Each call creates a new Session object and returns * Getdefaultins Tance () The first call returns a Session object and sets the session object to the default value, and when the method is called the second time, the default session object is returned **///Because the authenticator class is an abstract class, it can only be Name Inner class form new one authenticatorThe subclass object and overrides its Getpasswordauthentication () method Session session = Session.getinstance (props, New Authenticator ( ) {protected Passwordauthentication getpasswordauthentication () {return new Passwordauthenticati
		    	     On ("xh216319", "Xiaoxiao");
		       }
				}
		       );
		     Set debug mode to print execution process session.setdebug (true);
			  Create a Message object that represents the content of the messages. Message msg = new MimeMessage (session,new fileinputstream ("C:\\Documents and settings\\administrator\\ desktop \ \
			   
		      Sendmultipartmessagedemo3.eml "));
			Transport.send (msg);
 }

}



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.