Joke Collection Website - Blessing messages - Java short message integration
Java short message integration
First of all, we should build the development environment of JavaMail and configure the related classes and packages of JavaMail in JDK. When building JavaMail development environment, you need mail.jar and activation.jar These two files can be downloaded from SUN's official website.
JavaMail supports STMP, POP3 and IMAP, and encapsulates e-mail functions such as mail object, sending, authentication and receiving.
When sending various types of mail, it is mainly applicable to the following categories:
1) session class. If users want to send e-mail, they first need to create an object of class Session, and use this object to create a mail object and specify the client properties of mail server authentication. Its class level is javax.mail.Session
2)internet address class. The address class of mail sending, the class hierarchy is javax.mail.internet.internetaddress, which inherits from the abstract class javax.mail.Address
3) Simulate message classes. Mail message class, its class hierarchy is javax.mail.internet.mimemessage, which inherits from the abstract class javax.mail.Message
4) Transportation. The mail sending class has a class hierarchy of javax.mail.Transport
5) Validator class. Authorizer class, JavaMail uses Authenticator class to access those protected resources through user name and password, where "resource" is the mail server. Its class hierarchy is javax.mail.Authenticator
6) Store category. Used to receive mail from the mail server, and its class hierarchy is javax.mail.Store
7) folder class. The mail folder class has a class hierarchy of javax.mail.Folder
Implementation process:
& lt% @ page import =“Java . io . *“% & gt;
& lt% @ page import =“Java . util . *“% & gt;
& lt%@ page imports = "javax.mail. *"% >
& lt%@ page imports = "javax.mail.internet. *"% >
& lt% @ page import =“javax . activation . *“% & gt;
& lt%
Try {
request . setcharacter encoding(“GB 23 12“);
string from = request . getparameter(“jname“);
string to = request . getparameter(“sname“);
string subject = request . getparameter(“title“);
string message text = request . getparameter(“message“);
string password = request . getparameter(“password“);
string S = request . getparameter(“jname“);
int n = s . index of(‘@‘);
int m = s . length();
string server = s . substring(n+ 1,m);
//Establish a mail session
Properties pro = new property ();
pro . put(“mail . smtp . host“,“SMTP。 +server);
pro . put(“mail . stmp . auth“,“true“);
session sess = session . getinstance(pro);
sess . set debug(true);
MimeMessage = new MimeMessage (Mess); //Create a new message object
//Set the sender
internet address from _ mail = new internet address(from);
message . set from(from _ mail);
//Set the recipient
internet address to _ mail = new internet address(to);
Message . set recipient(Message。 Recipient type. Recipient, Recipient _ Mail);
Message.setSubject (topic); //Set the theme
message . settext(message text); //Set the content
message . setsentdate(new Date()); //Set the sending time
//Send mail
message . save changes(); //Save e-mail information
transport transport = sess . get transport(“SMTP“);
Transport.connect ("SMTP+server, sender, password);
transport . sendmessage(message,message . getallrecipients());
transport . close();
Out.print ("Mail sent successfully");
}catch (exception e ){
Out.print ("Sending email failed, probably because.
out . println(e . getmessage());
}
% & gt
2. Send e-mail in HTML format
The MimeMultipart object is used to store the specific content of the HTML file, and the format of the object should be set when setting the content.
The class hierarchy of MimeMultipart class is javax.mail.internet.mimeMultipart. The universal container for storing e-mail content is a multipart abstract class, which defines the methods of adding, deleting and obtaining different contents of e-mail. Because Multipart is an abstract class, a concrete subclass must be used. JavaMail API provides javax.mail.internet.mimemultipart class to use MimeMessage object.
Grammar:
multipart mul = new mime multipart();
Description: The commonly used method when using MimeMultipart object is addBodyPart (), which can add BodyPart object to the content of e-mail. A message can have many parts, and a body part can represent one part.
The class hierarchy of the MimeBodyPart class is javax.mail.internet.mimebody part. Mimebodypart is a subclass of bodypart specific to MimeMessage. The MimeBodyPart object represents a part of the contents of the MimeMessage object, and each MimeBodyPart is considered to be composed of two parts: the MIME type and the content matching the type.
Grammar:
body part MDP = new mime body part(); //Create a new BodyPart object to store the contents of the letter.
string message text =“Hello World! “;
//define the MIME type as text/html, and set the contents of MimeBodyPart.
MDP . set content(message text,“text/html“);
Implementation process:
& lt% @ page import =“Java . io . *“% & gt;
& lt% @ page import =“Java . util . *“% & gt;
& lt%@ page imports = "javax.mail. *"% >
& lt%@ page imports = "javax.mail.internet. *"% >
& lt% @ page import =“javax . activation . *“% & gt;
& lt%
Try {
request . setcharacter encoding(“GB 23 12“);
string from _ mail = request . getparameter(“sname“);
string to _ mail = request . getparameter(“rname“);
string subject = request . getparameter(“title“);
string message text = request . getparameter(“message“);
string password = request . getparameter(“password“);
string S = request . getparameter(“sname“);
int n = s . index of(‘@‘);
int m = s . length();
string server = s . substring(n+ 1,m);
Properties prop = new Properties();
prop . put(“mail . smtp . host“,“SMTP。 "+server);
prop . put(“mail . SMTP . auth“,“true“);
session sess = session . getinstance(prop);
sess . set debug(true);
MimeMessage = new MimeMessage (SESS);
//Set the recipient, sender, subject and sending time for the message object.
internet address from = new internet address(from _ mail);
Message.setFrom (from);
InternetAddress to = new Internet address (to _ mail);
Message . set recipient(Message。 Recipient type. Recipient, addressee);
Message.setSubject (topic);
message . setsentdate(new Date());
Multipart mul = new Multipart(); //Create a new MimeMultipart object to store multiple BodyPart objects.
body part MDP = new mime body part(); //Create a new BodyPart object to store the contents of the letter.
MDP . set content(message text,“text/html; charset = GB 23 12“);
mul . addbodypart(MDP); //Add a BodyPart containing alphabetic content to the MimeMultipart object.
message . save changes();
transport transport = sess . get transport(“SMTP“);
Transport.connect ("SMTP+server, sender's mail, password);
transport . sendmessage(message,message . getallrecipients());
transport . close();
Out.println ("Mail sent successfully! “);
}catch (exception e ){
Out.print ("Sending email failed, probably because.
out . println(e . getmessage());
}
% & gt
3. Send an email with an attachment
Generally, when designing a mail sender with attachments, you can follow the following steps:
1) To send an e-mail with attachments, it is necessary to establish each e-mail body part of the e-mail, and add attachments with DataHandler after the first part (that is, the content text of the e-mail).
2) If the file is sent as an attachment, an object of type FileDataSource should be established as the attachment data source; If you read data from a URL and send it as an attachment, you should create an object of type URLDataSource as the attachment data source.
3) Pass in a data source (FileDataSource or URLDataSource) object as the parameter of the constructor of the DataHandler class, thus establishing a DataHandler object as the DataHandler of the data source.
4) Set this DataHandler as the DataHandler of the email body, thus completing the association between the email body and the attachment. The following work is to use the setFileName () method of BodyPart to set the attachment name to the original file name.
5) Put the two email bodies into a multipart, set the email content as a multipart of the container, and send the email.
Implementation process:
& lt% @ page import =“Java . io . *“% & gt;
& lt% @ page import =“Java . util . *“% & gt;
& lt%@ page imports = "javax.mail. *"% >
& lt%@ page imports = "javax.mail.internet. *"% >
& lt% @ page import =“javax . activation . *“% & gt;
& lt% @ page import =“Java . net . *“% & gt;
& lt%
Try {
request . setcharacter encoding(“GB 23 12“);
string from _ mail = request . getparameter(“sname“);
sting to _ mail = request . getparameter(“rname“);
string from _ PSD = request . getparameter(“password“);
string title = request . getparameter(“title“);
string content = request . getparameter(“content“);
string path = request . getparameter(“attachment“);
string S = request . getparameter(“sname“);
int n = s . index of(‘@‘);
int m = s . length();
string server = s . substring(n+ 1,m);
Properties prop = new Properties();
prop . put(“mail . smtp . host“,“SMTP。 "+server);
prop . put(“mail . SMTP . auth“,“true“);
session sess = session . getinstance(prop);
session . set debug(true);
MimeMessage = new MimeMessage (SESS);
//Set the recipient, sender and subject for the message object.
internet address from = new internet address(from _ mail);
Message.setFrom (from);
InternetAddress to = new Internet address (to _ mail);
Message . set recipient(Message。 Recipient type. Recipient, addressee);
Message.setSubject (title);
multipart mul = new mime multipart(); //Create a new MimeMultipart object to store multiple BodyPart objects.
body part MDP = new mime body part(); //Create a new BodyPart object to store the contents of the letter.
MDP . set content(content,“text/html; charset = GB 23 12“);
mul . addbodypart(MDP); //Add a BodyPart containing alphabetic content to the MimeMultipart object.
//Set the attachment of the letter
MDP = new mime body part(); //Create a new BodyPart for storing attachments.
Datahandlerhandler = newdatahandler ("JavaMail attachment test", "text/plain;; charset = GB 23 12“); //Create a new DataHandler object and set its content, format and encoding method.
MDP . set filename(“mrsoft“); //may be inconsistent with the source file name.
Mdp.setDataHandler (handler);
mul . addbodypart(MDP);
message . set content(mul); //Use mul as the content of the message object.
message . save changes();
transport transport = sess . get transport(“SMTP“);
transport . connect(“SMTP+server,from_mail,from _ PSD);
transport . sendmessage(message,message . getallrecipients());
transport . close();
Out.println ("Attachment sent successfully! “);
}catch (exception) {
Out.print ("The email failed, probably because:
out . println(ex . getmessage());
}
% & gt
Step 4 send emails in groups
Set the recipient's address to the form of tomail+i, and use for loop to send the mail to these addresses, so as to achieve the purpose of mass mailing.
Use the Address class to set the recipient and sender information of e-mail messages. After creating the e-mail address class, set the sender of the e-mail through the setFrom () method of message. The code is as follows:
message . set from(from _ mail);
When setting the recipient address, use the setRecipient () method to set the recipient address. The code is as follows:
Message.setRecipient (type, address);
The parameter type is the recipient type. You can use the following three constants to distinguish the types of recipients:
1) message. Recipient type. To be sent.
2) mail. Recipient type. Cc
3)message. recipient type. bcc- secret sending.
Note: In daily use, we often use two forms of mail sending: CC and BCC. Both cc and bcc send e-mail to multiple mailboxes at the same time, but they
There are some differences between them. The difference is that secret delivery hides the addresses of other cc's and only allows the recipients to see their own addresses, which can play a role in keeping secrets and preventing spam.
Implementation process:
& lt% @ page import =“Java . io . *“% & gt;
& lt% @ page import =“Java . util . *“% & gt;
& lt%@ page imports = "javax.mail. *"% >
& lt%@ page imports = "javax.mail.internet. *"% >
& lt% @ page import =“javax . activation . *“% & gt;
& lt%
Try {
int I = 1;
for(I = 1; I<4; i++){
request . setcharacter encoding(“GB 23 12“);
string from = request . getparameter(“from _ mail“);
string to = request . getparameter(“to mail“+I);
string subject = request . getparameter(“subject“);
string message text = request . getparameter(“message“);
string password = request . getparameter(“password“);
string S = request . getparameter(“from _ mail“);
int n = s . index of(‘@‘);
int m = s . length();
string server = s . substring(n+ 1,m);
//Establish a mail session
Properties pro = new property ();
pro . put(“mail . smtp . host“,“SMTP。 +server);
pro . put(“mail . SMTP . auth“,“true“);
session sess = session . getinstance(pro);
sess . set debug(true);
MimeMessage = new MimeMessage (SESS); //Create a new message object
internet address from _ mail = new internet address(from); //Set the sender
message . set from(from _ mail);
//Set the recipient
internet address to = new internet address(to);
Message . set recipient(Message。 Recipient type. Recipient, Recipient _ Mail);
Message.setSubject (topic); //Set the theme
message . settext(message text); //Set the content
message . setsentdate(new Date()); //Set the sending time
message . save changes(); //Save e-mail information
transport transport = sess . get transport(“SMTP“);
Transport.connect ("SMTP+server, sender, password);
transport . sendmessage(message,message . getallrecipients());
transport . close();
if(I = = 3 ){
Out.print ("Email sent successfully");
}
}
}catch (exception e ){
Out.print ("Sending email failed, probably because.
out . println(e . getmessage());
}
% & gt
5.Spring uses WebService to send short messages.
Sending short messages through programming is a complicated matter. At present, the general solution is to use the programming language of mobile phone to write short message programs through the connection between computer and mobile phone. This method not only needs time and energy, but also needs corresponding hardware facilities and must understand the relevant knowledge of mobile phone programming. You can remotely access the existing short message sending server through Web service technology, and send short messages by calling the server.
Technical points:
Use existing resources: Web service remote service that can send short messages. This Web service is provided by Sina website, which allows users to directly call remote services to send messages. The service name of the service is SMSWS, and the service port is SMSWebServiceSoapPort. In this remote service, a method of sending short messages is provided, sendXml (). The method is defined as follows:
Common string sendXml (string carrier, string id, string password, string toMobile, string message, string msgtype)
Function: Send SMS to the target user's mobile phone according to the parameters given by the user.
Parameter description:
The six parameters in the sendXml () method are all string types, and the return value of the sendXml () method is also string type. The following is a detailed description of the six parameters in the sendXml () method.
1) operator: operator name. There is no specific requirement for the specific use of this parameter, that is, you can enter it at will, and the input string will not be displayed in the other party's mobile phone.
2)id: the mobile phone number registered in Sina. When using Sina service to send short messages, you need to register your mobile phone on Sina website.
3) Password: the password sent back by Sina.com after the successful mobile phone registration.
4)toMobile: the mobile phone number of the other party receiving the short message.
5) Message: the content of the short message to be sent.
6)msg type: the type of sending short messages. Since it is not a multimedia message, please enter text.
Calling the sendXml () method requires configuring an instance of JaxRpcPortProxyFactoryBean in Spring, and then defining the interface containing the method, and Spring will automatically obtain the interface of the remote service.
Implementation process:
application context AC = new classpathmlaplicationcontext(“application context . XML“);
SMSWebService sendSms =(SMSWebService)AC . get bean(“hello world service“);
string carrier =“lzwsky“;
string id = userid . gettext();
string password = password field . gettext();
string toMobile = tomobilefield . gettext();
string message = message field . gettext();
string msg type =“Text“;
SendSms.sendXml (operator, id, password, toMobile, message, msgtype);
- Previous article:How to cancel the SMS notification of Three Kingdoms Kill login?
- Next article:Leave a message for the old lover's thoughts.
- Related articles
- New Year's greetings
- Delivery center limit notification SMS
- A short sentence about mom's copywriting
- Why can't Xiaomi mobile phone send text messages regularly?
- How to write the recruitment information of hotel employees
- How to make a BlackBerry 9000 phone?
- Best wishes to teachers on May Day.
- Love letters and text messages that touched his girlfriend.
- Do you have a good message for blessing the Dragon Boat Festival?
- How to cancel 95598 SMS notification