Java mail entry instance

Source: Internet
Author: User

The first class smtpauth:

 

import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;public class SmtpAuth extends Authenticator {private String username, password;public SmtpAuth(String username, String password) {this.username = username;this.password = password;}protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}}

The second mail type:

Import Java. util. date; import Java. util. enumeration; import Java. util. hashmap; import Java. util. properties; import Java. util. vector; import javax. activation. datahandler; import javax. activation. filedatasource; import javax. mail. address; import javax. mail. authenticationfailedexception; import javax. mail. message; import javax. mail. messagingexception; import javax. mail. multipart; import javax. mail. session; import Javax. mail. transport; import javax. mail. internet. internetaddress; import javax. mail. internet. mimebodypart; import javax. mail. internet. mimemessage; import javax. mail. internet. mimemultipart; public class mail {// The name displayed by the sender: Private string displayname; // the recipient's private string to; // the sender's address: Private string from; // SMTP server address private string smtpserver; // Mail User Name private string username; // Mail User Password private string password ;/ /Email Subject name private string subject; // email content private string content; private Boolean ifauth; // whether the server requires Identity Authentication // the file path of the attachment is private string filename = ""; private Vector file = new vector (); // a collection of file names used to save the attachments sent/*** set SMTP server address */Public void setsmtpserver (string smtpserver) {This. smtpserver = smtpserver;}/*** set the sender address */Public void setfrom (string from) {This. from = from;}/*** set the name displayed by the sender */Public void setdisplaynam E (string displayname) {This. displayname = displayname;}/*** sets whether the server requires Identity Authentication */Public void setifauth (Boolean ifauth) {This. ifauth = ifauth;}/*** set the email user name */Public void setusername (string username) {This. username = username;}/*** set the email password */Public void setpassword (string password) {This. password = password;}/*** set receiver */Public void setto (string to) {This. to = to;}/*** set Topic */Public void Sets Ubject (string subject) {This. subject = subject;}/*** set subject content */Public void setcontent (string content) {This. content = content;}/*** this method is used to collect the attachment name */Public void addattachfile (string fname) {file. addelement (fname);} public mail () {}/*** initialize the SMTP server address, Sender's e-mail address, sender's name, user name, password, receiver, subject, and content */public mail (string smtpserver, string from, string displayname, string username, string password, string to, string s Ubject, string content) {This. smtpserver = smtpserver; this. from = from; this. displayname = displayname; this. ifauth = true; this. username = username; this. password = password; this. to = to; this. subject = subject; this. content = content;}/*** initialize the SMTP server address, Sender's e-mail address, sender's name, receiver, subject, and content */public mail (string smtpserver, string from, string displayname, string to, string subject, string content) {This. smtpser Ver = smtpserver; this. from = from; this. displayname = displayname; this. ifauth = false; this. to = to; this. subject = subject; this. content = content;}/*** send email */Public hashmap send () {hashmap map = new hashmap (); map. put ("State", "success"); string message = "the email is sent successfully! "; Session session = NULL; properties props = system. getproperties (); props. put ("mail. SMTP. host ", smtpserver); If (ifauth) {// The server requires authentication props. put ("mail. SMTP. auth "," true "); smtpauth = new smtpauth (username, password); Session = session. getdefaultinstance (props, smtpauth);} else {props. put ("mail. SMTP. auth "," false "); Session = session. getdefaultinstance (props, null);} session. setdebug (true); Transp ORT trans = NULL; try {message MSG = new mimemessage (session); try {address from_address = new internetaddress (from, displayname); MSG. setfrom (from_address);} catch (Java. io. unsupportedencodingexception e) {e. printstacktrace ();} internetaddress [] address = {New internetaddress (to)}; MSG. setrecipients (message. recipienttype. to, address); MSG. setsubject (subject); multipart MP = new mimemultipart (); mimebod Ypart MBP = new mimebodypart (); MBP. setcontent (content. tostring (), "text/html; charset = gb2312"); MP. addbodypart (MBP); If (! File. isempty () {// There is an attachment enumeration efile = file. elements (); While (efile. hasmoreelements () {MBP = new mimebodypart (); filename = efile. nextelement (). tostring (); // select the name of each attachment filedatasource FDS = new filedatasource (filename); // obtain the data source MBP. setdatahandler (New datahandler (FDS); // obtain the attachment and import it to bodypartmbp. setfilename (FDS. getname (); // get the same name as bodypartmp. addbodypart (MBP);} file. removeallelements ();} MSG. SETCO Ntent (MP); // Add the multipart to the Message MSG. setsentdate (new date (); // set the sending date of the mail header // send the mail MSG. savechanges (); Trans = session. gettransport ("SMTP"); Trans. connect (smtpserver, username, password); Trans. sendmessage (MSG, MSG. getallrecipients (); Trans. close ();} catch (authenticationfailedexception e) {map. put ("State", "failed"); message = "failed to send email! Error cause: \ n "+" authentication error! "; E. printstacktrace ();} catch (messagingexception e) {message =" failed to send the email! Error cause: \ n "+ E. getmessage (); map. put ("State", "failed"); E. printstacktrace (); Exception EX = NULL; If (EX = E. getnextexception ())! = NULL) {system. out. println (ex. tostring (); Ex. printstacktrace () ;}// system. out. println ("\ n prompt:" + message); map. put ("message", message); Return map;} public static void main (string [] ARGs) {mail = new mail (); mail. setsmtpserver ("smtp.126.com"); mail. setfrom ("xxx@126.com"); mail. setdisplayname ("test email"); mail. setifauth (true); mail. setusername ("XXX"); mail. setpassword ("yyy"); mail. setto ("654476371@qq.com "); Mail. setsubject ("Haha"); mail. setcontent ("this is the mail content. "); Mail. addattachfile (" F: // Kankan // description .txt "); mail. Send ();}}

With the above two classes, you can.

 

Modify the xxx@126.com for your mailbox account, XXX For the user name, YYY for your mailbox password.

 

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.