The following is an encapsulation of the mail sending function. using these methods, you can easily send emails or even add attachments.
Import java. util. Date;
Import java. util. Properties;
Import javax. activation. CommandMap;
Import javax. activation. DataHandler;
Import javax. activation. DataSource;
Import javax. activation. FileDataSource;
Import javax. activation. MailcapCommandMap;
Import javax. mail. BodyPart;
Import javax. mail. Multipart;
Import javax. mail. PasswordAuthentication;
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 extends javax. mail. Authenticator {
Private String _ user;
Private String _ pass;
Private String [] _;
Private String _ from;
Private String _ port;
Private String _ sport;
Private String _ host;
Private String _ subject;
Private String _ body;
Private boolean _ auth;
Private boolean _ debuggable;
Private Multipart _ multipart;
Public Mail (){
_ Host = "smtp.gmail.com"; // default smtp server
_ Port = "465"; // default smtp port
_ Sport = "465"; // default socketfactory port
_ User = ""; // username
_ Pass = ""; // password
_ From = ""; // email sent from
_ Subject = ""; // email subject
_ Body = ""; // email body
_ Debuggable = false; // debug mode on or off-default off
_ Auth = true; // smtp authentication-default on
_ Multipart = new MimeMultipart ();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap. getdefacommandcommandmap ();
Mc. addMailcap ("text/html; x-java-content-handler = com. sun. mail. handlers. text_html ");
Mc. addMailcap ("text/xml; x-java-content-handler = com. sun. mail. handlers. text_xml ");
Mc. addMailcap ("text/plain; x-java-content-handler = com. sun. mail. handlers. text_plain ");
Mc. addMailcap ("multipart/*; x-java-content-handler = com. sun. mail. handlers. multipart_mixed ");
Mc. addMailcap ("message/rfc822; x-java-content-handler = com. sun. mail. handlers. message_rfc822 ");
CommandMap. setdefacommandcommandmap (mc );
}
Public Mail (String user, String pass ){
This ();
_ User = user;
_ Pass = pass;
}
Public boolean send () throws Exception {
Properties props = _ setProperties ();
If (! _ User. equals ("")&&! _ Pass. equals ("") & _ to. length> 0 &&! _ From. equals ("")&&! _ Subject. equals ("")&&! _ Body. equals ("")){
Session session = Session. getInstance (props, this );
MimeMessage msg = new MimeMessage (session );
Msg. setFrom (new InternetAddress (_ from ));
InternetAddress [] addressTo = new InternetAddress [_ to. length];
For (int I = 0; I <_ to. length; I ++ ){
AddressTo [I] = new InternetAddress (_ to [I]);
}
Msg. setRecipients (MimeMessage. RecipientType. TO, addressTo );
Msg. setSubject (_ subject );
Msg. setSentDate (new Date ());
// Setup message body
BodyPart messageBodyPart = new MimeBodyPart ();
MessageBodyPart. setText (_ body );
_ Multipart. addBodyPart (messageBodyPart );
// Put parts in message
Msg. setContent (_ multipart );
// Send email
Transport. send (msg );
Return true;
} Else {
Return false;
}
}
Public void addAttachment (String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart ();
DataSource source = new FileDataSource (filename );
MessageBodyPart. setDataHandler (new DataHandler (source ));
MessageBodyPart. setFileName (filename );
_ Multipart. addBodyPart (messageBodyPart );
}
@ Override
Public PasswordAuthentication getPasswordAuthentication (){
Return new PasswordAuthentication (_ user, _ pass );
}
Private Properties _ setProperties (){
Properties props = new Properties ();
Props. put ("mail. smtp. host", _ host );
If (_ debuggable ){
Props. put ("mail. debug", "true ");
}
If (_ auth ){
Props. put ("mail. smtp. auth", "true ");
}
Props. put ("mail. smtp. port", _ port );
Props. put ("mail. smtp. socketFactory. port", _ sport );
Props. put ("mail. smtp. socketFactory. class", "javax.net. ssl. SSLSocketFactory ");
Props. put ("mail. smtp. socketFactory. fallback", "false ");
Return props;
}
// The getters and setters
Public String getBody (){
}
Public void setBody (String _ body ){
This. _ body = _ body;
}
// More of the getters and setters .....
}
And now I'm going to go through each bit of code
Public Mail (){
_ Host = "smtp.gmail.com"; // default smtp server
_ Port = "465"; // default smtp port
_ Sport = "465"; // default socketfactory port
_ User = ""; // username
_ Pass = ""; // password
_ From = ""; // email sent from
_ Subject = ""; // email subject
_ Body = ""; // email body
_ Debuggable = false; // debug mode on or off-default off
_ Auth = true; // smtp authentication-default on
_ Multipart = new MimeMultipart ();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap. getdefacommandcommandmap ();
Mc. addMailcap ("text/html; x-java-content-handler = com. sun. mail. handlers. text_html ");
Mc. addMailcap ("text/xml; x-java-content-handler = com. sun. mail. handlers. text_xml ");
Mc. addMailcap ("text/plain; x-java-content-handler = com. sun. mail. handlers. text_plain ");
Mc. addMailcap ("multipart/*; x-java-content-handler = com. sun. mail. handlers. multipart_mixed ");
Mc. addMailcap ("message/rfc822; x-java-content-handler = com. sun. mail. handlers. message_rfc822 ");
CommandMap. setdefacommandcommandmap (mc );
}
Public Mail (String user, String pass ){
This ();
_ User = user;
_ Pass = pass;
}
In this Code, we initialized the mail-like attribute and set it to the default value.
Note the JavaMail MIME type. There is a description.
Take a closer look at the two constructors. You can use one of them to pass in the username and password.
Public boolean send () throws Exception {
Properties props = _ setProperties ();
If (! _ User. equals ("")&&! _ Pass. equals ("") & _ to. length> 0 &&! _ From. equals ("")&&! _ Subject. equals ("")&&! _ Body. equals ("")){
Session session = Session. getInstance (props, this );
MimeMessage msg = new MimeMessage (session );
Msg. setFrom (new InternetAddress (_ from ));
InternetAddress [] addressTo = new InternetAddress [_ to. length];
For (int I = 0; I <_ to. length; I ++ ){
AddressTo [I] = new InternetAddress (_ to [I]);
}
Msg. setRecipients (MimeMessage. RecipientType. TO, addressTo );
Msg. setSubject (_ subject );
Msg. setSentDate (new Date ());
// Setup message body
BodyPart messageBodyPart = new MimeBodyPart ();
MessageBodyPart. setText (_ body );
_ Multipart. addBodyPart (messageBodyPart );
// Put parts in message
Msg. setContent (_ multipart );
// Send email
Transport. send (msg );
Return true;
} Else {
Return false;
}
}
This is the sending method. The data attribute is set and the sending operation is executed.
Public void addAttachment (String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart ();
DataSource source = new FileDataSource (filename );
MessageBodyPart. setDataHandler (new DataHandler (source ));
MessageBodyPart. setFileName (filename );
_ Multipart. addBodyPart (messageBodyPart );
}
This is the function for adding attachments, but it must be called before sending.
Private Properties _ setProperties (){
Properties props = new Properties ();
Props. put ("mail. smtp. host", _ host );
If (_ debuggable ){
Props. put ("mail. debug", "true ");
}
If (_ auth ){
Props. put ("mail. smtp. auth", "true ");
}
Props. put ("mail. smtp. port", _ port );
Props. put ("mail. smtp. socketFactory. port", _ sport );
Props. put ("mail. smtp. socketFactory. class", "javax.net. ssl. SSLSocketFactory ");
Props. put ("mail. smtp. socketFactory. fallback", "false ");
Return props;
}
Here, we set the mail authentication Attribute-SMTP authentication by default.
This is a connection to all Gmail (Google) SMTP servers.