JavaMail Send Mail with Accessories

xiaoxiao2021-03-06  44

Package com.mail;

Import Java.io. *; import java.util. *; import javax.mail. *; import javax.mail.internet. *; import java.io.printwriter; import java.util.properties Import javax.naming.context; import javax.naming.initialcontext; import javax.activation. *; import java.io ioException;

Public class sendtest {public static class smtpauthenticator extens authenticator {private string username; private string password;

Public SMTPAUTHENTICATOR (String Username, String Password) {super (); this.username = username; this.password = password;}

Protected passwordAuthentication () {return new passwordauthentication (this.username, this.password);}}

/ * * E-mail * * @param smtpHost * * @param email * * @throws MessagingException * / protected static void sendMail (String email) throws MessagingException {try {Properties props = System.getProperties (); props.put ( "mail .smtp.host "," mail.xxx.com.cn "); // SMTP server Props.put (" mail.smtp.auth "," true "); // Do you need to verify your identity when sending an email? ("mail.mime.charset", "gb2312"); // message character set string username = "xxxx"; // Mailbox User name string password = "xxxx"; // mailbox password SMTPAUTICATOR Authenticator = New SMTPAUTHENTICATOR ( Username, password); // User authentication // crete a new session Object session session = session.getInstance (props, authenticator); session.setdebug (false); // Whether to print debug information Tags String from = "xxxx @ xxxx.com.cn "; // Send Mailbox String to = Email; // Receiver String Subject =" xxxxxx "; // Email Title // Create A New MimeMessage Object (Using The session created Above MimeMessage Message = New MimeMessage (session); Message.SetFrom (New InternetDress (from, "xxxx", "GB231 2 ")); InternetAddress [] address = new internetdress [] {new internetaddress (to," Dear User "," GB2312 ")}; message.recipientType.to, Address; // String File = "/home/qianglee/mail/user/datasmall/2005-01-11/xxxx/1105454895791.png"; // Create your new message part BodyPart messageBodyPart = new MimeBodyPart (); String mailTitle = " ";

String soilfoot = " "; string PBG = "

"; string PED = "";

String fonttmp = "Chinese test"; string fonttmp2 = "graphic mixing test 1 graphic mixing test 1 graphic mixing test 1 graphic mixing test 1 graphic mixing test 1 graphic mixing test 1"; String fonttmp3 = "graphic mixing test 2 graphic mixed tester 2 graphic mixing test 2 graphic mixing test 2 graphic mixing test 2 graphic mixing test 2";

fonttmp = new String (fonttmp.getBytes ( "GB2312"), "ISO8859_1"); fonttmp2 = new String (fonttmp2.getBytes ( "GB2312"), "ISO8859_1"); fonttmp3 = new String (fonttmp3.getBytes ( "GB2312" ), "ISO8859_1"); string body = mailtitle PBG FONTTMP PED; //Http://220.194.61.227/USER/Data/2005-01-21/13311010093/1106278749591.png string file1 = "/ home / Qianglee / Mail / USER / DATA / 2005-01-11 / XXX / 1105454895791.PNG "; string file2 =" /Home/qianglee/mail/user/data/2005-01-21/xxxx/1106278749591.png ";

String file3 = "/Home/qianglee/mail/user/data/2005-01-21/xxxx/1106278745321.png"; body = pbg "" PED; Body = PBG FONTTMP2 PED;

Body = PBG " PED; body = PBG FONTTMP3 PED;

Body = PBG " PED; body = mailfoot; messagebodypart.setcontent (body," text / html "); // Create a Related Multi-Part to Combine the parts MimeMultipart multipart = new MimeMultipart ( "related"); multipart.addBodyPart (messageBodyPart); // Create part for the image for (int i = 1; i <4; i ) {messageBodyPart = new MimeBodyPart (); String t = ""; ((I-1) == 0) T = file1; IF ((i-2) == 0) T = file2; if ((i-3) == 0) T = file3; / / Fetch the image and associate to part DataSource fds = new FileDataSource (t); // DataSource fds2 = new FileDataSource (file2); // DataSource fds3 = new FileDataSource (file3); messageBodyPart.setDataHandler (new DataHandler (fds)); //messageBodyPart.setDataHandler(new DataHandler (fds2)); //messageBodyPart.setDataHandler(new DataHandler (fds3)); String tm = "mememe" i; messageBodyPart.setHeader ( "Content-ID", tm); // MessageBodypart.setHeader ("Content-ID", "MEMEME2"); //messageBodyPart.setHeader("Content-ID","mememe3 "); // Add part to multipart multipart.addBodyPart (messageBodyPart); // Associate multipart with message message.setContent (multipart);} // If you do not need to take an accessory, use the following two lines of code to //Message.SetContent (Body, "Text / HTML"); //message.settext (Body, "GB2312"); transport.send (Message); System .out.println ("------ mail is sent to [" to "] success ------ / n");

} catch (throwable t) {system.out.println ("----- mail does not send success ------ / n"); T.PrintStackTrace (System.out);}} public static Void Main (String Args []) throws exception {thread.sleep (500); sendmail ("xxxxxxxxxx@xxxx.com.cn"); thread.sleep (500);}}

转载请注明原文地址:https://www.9cbs.com/read-57828.html

New Post(0)