Joke Collection Website - Public benefit messages - The response returned by spring-ws using webservicemessage on the client is output to the console. The content is as follows:

The response returned by spring-ws using webservicemessage on the client is output to the console. The content is as follows:

1. Send email

First, you should build a JavaMail development environment and configure JavaMail related classes and packages in the JDK. In building the JavaMail development environment, two files, mail.jar and activation.jar, are required. These two files can be downloaded from SUN's official website.

JavaMail provides support for STMP, POP3, and IMAP, and encapsulates mail objects, sending, authentication, receiving and other functions in the email function.

When sending various types of emails, the following categories are mainly applied:

1) Session class. If a user wants to send an email, he first needs to create an object of the Session class, use this object to create an email object, and specify the client attributes for mail server authentication. Its class hierarchy is javax.mail.Session.

2) InternetAddress class. The address class for sending emails. The class hierarchy is javax.mail.internet.InternetAddress, which inherits from the abstract class javax.mail.Address.

3) MimeMessage class. Mail message class, its class hierarchy is javax.mail.internet.MimeMessage, inherited from the abstract class javax.mail.Message.

4) Transport class. Mail sending class, its class hierarchy is javax.mail.Transport.

5) Authenticator class. Authorizer class, JavaMail uses the Authenticator class to access protected resources in the form of user name and password. The "resource" here is the mail server. Its class hierarchy is javax.mail.Authenticator.

6) Store class. Used to receive mail from the mail server, its class hierarchy is javax.mail.Store.

7) Folder class. Mail folder class, its class hierarchy is javax.mail.Folder.

Implementation process:

<%@ page import="java.io.*"%>

<%@ page import="java.util. *"%>

<%@ page import="javax.mail.*"%>

<%@ page import="javax.mail.internet.*"%>

<%@ page import="javax.activation.*"%>

<%

try{

request.setCharacterEncoding ("gb2312");

String from = request.getParameter("jname");

String to = request.getParameter("sname");

String subject = request.getParameter("title");

String messageText = request.getParameter("message");

String password = request.getParameter("password") ;

String S = request.getParameter("jname");

int n = S.indexOf('@');

int m = S .length();

String server = S.substring(n+1,m);

//Establish a mail session

Properties pro = new Properties ();

pro.put("mail.smtp.host","smtp."+server);

pro.put("mail.stmp.auth"," true");

Session sess = Session.getInstance(pro);

sess.setDebug(true);

MimeMessage message = new MimeMessage(mess) ;//Create a new message object

//Set the sender

InternetAddress from_mail = new InternetAddress(from);

message.setFrom(from_mail);

//Set the recipient

InternetAddress to_mail = new InternetAddress(to);

message.setRecipient(Message.RecipientType.TO,to_mail);< /p>

message.setSubject(subject);//Set the subject

message.setText(messageText);//Set the content

message.setSentDate(new Date() );//Set sending time

//Send mail

message.saveChanges();//Save mail information

Transport transport = sess.g

etTransport("smtp");

transport.connect("smtp."+server,from,password);

transport.sendMessage(message,message.getAllRecipients());

transport.close();

out.print("Email sent successfully");

}catch(Exception e){

< p> out.print("Failed to send email, the reason may be
    ");

    out.println(e.getMessage());

    }

    %>

    2. Send emails in HTML format

    The MimeMultipart object is used to store the specific content of the HTML file. When setting the content, the format of the object should be set at the same time.

    The class hierarchy of the MimeMultipart class is javax.mail.Internet.MimeMultipart. The container that generally saves email content is the Multipart abstract class, which defines methods for adding, deleting, and obtaining different content of emails. Since Multipart is an abstract class, a specific subclass must be used. The JavaMail API provides the javax.mail.Internet.MimeMultipart class to use MimeMessage objects.

    Syntax:

    Multipart mul = new MimeMultipart();

    Description: A commonly used method when using MimeMultipart objects is addBodyPart(), which can be used in electronic Add a BodyPart object to the content of the email. The message can have many parts, and one BodyPart can represent one part.

    The class hierarchy of the MimeBodyPart class is javax.mail.Internet.MimeBodyPart. MimeBodyPart is a subclass of BodyPart specific to MimeMessage. A MimeBodyPart object represents a portion of the content of a MimeMessage object. Each MimeBodyPart is considered to consist of two parts: a MIME type and content matching this type.

    Syntax:

    BodyPart mdp = new MimeBodyPart();//Create a new BodyPart object to store the content of the letter

    String messageText = "Hello World!";

    //Define the MIME type as text/html and set the content of MimeBodyPart

    mdp.setContent(messageText, "text/html");

    Implementation Process:

    <%@ page import="java.io.*"%>

    <%@ page import="java.util.*"%>

    <%@ page import="javax.mail.*"%>

    <%@ page import="javax.mail.internet.*"%>

    < %@ page import="javax.activation.*"%>

    <%

    try{

    request.setCharacterEncoding("gb2312");

    String from_mail = request.getParameter("sname");

    String to_mail = request.getParameter("rname");

    String subject = request.getParameter( "title");

    String messageText = request.getParameter("message");

    String password = request.getParameter("password");

    String S = request.getParameter("sname");

    int n = S.indexOf('@');

    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.setDebug(true);

    MimeMessage message = new MimeMessage(sess);

    //Set the recipient and sender for the message object Person, subject, sending time

    InternetAddress from = new InternetAddress(from_mail);

    message.setFrom(from);

    InternetAddress to = new InternetAddress( to_mail);

    message.setRecipient(Message.RecipientType.TO,to);

    message.setSubject(subj

    ect);

    message.setSentDate(new Date());

    Multipart mul = new Multipart();//Create a new MimeMultipart object to store multiple BodyPart objects

    BodyPart mdp = new MimeBodyPart();//Create a new BodyPart object to store the content of the message

    mdp.setContent(messageText,"text/html;charset=gb2312");

    mul.addBodyPart(mdp);//Add the BodyPart containing the message content to the MimeMultipart object

    message.saveChanges();

    Transport transport = sess.getTransport( "smtp");

    transport.connect("smtp."+server,from_mail,password);

    transport.sendMessage(message,message.getAllRecipients());

    p>

    transport.close();

    out.println("Email sent successfully!");

    }catch(Exception e){

    out.print("Failed to send email, the reason may be

      ");

      out.println(e.getMessage());

      }

      < p>%>

      3. Sending emails with attachments

      Generally, you can follow the following steps when designing a program to send emails with attachments:

      1 ) To send an email with an attachment, you need to create each email body part of the email, and add an attachment with a DataHandler after the first part (ie, the email content text).

      2) If the file is sent as an attachment, an object of FileDataSource type must be established as the attachment data source; if data is read from the URL and sent as an attachment, an object of URLDataSource type must be established as the attachment data source.

      3) Pass in the data source (FileDataSource or URLDataSource) object as a parameter of the constructor method of the DataHandler class, thereby 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 attachments. 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 Multipart, set the email content to the Multipart of this container, and send the email.

      Implementation process:

      <%@ page import="java.io.*"%>

      <%@ page import="java.util. *"%>

      <%@ page import="javax.mail.*"%>

      <%@ page import="javax.mail.internet.*"%>

      <%@ page import="javax.activation.*"%>

      <%@ page import="java.net.*"%>

      <%

      try{

      request.setCharacterEncoding("gb2312");

      String from_mail = request.getParameter("sname");

      Stirng 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("attachement");

      String S = request.getParameter("sname");

      int n = S.indexOf('@');

      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.setDebug(true);

      MimeMessage message = new MimeMessage(sess);

      //Set the recipient and sender for the message object , topic

      InternetAddress from = new InternetAddress(from_mail);

      message.setFrom(from);

      InternetAddress to = new InternetAddress(to_mail);

      p>

      message.setRecipient(Message.RecipientType.TO,to);

      message.setSubject(title);

      Multipart mul = new MimeMultipart();//New A MimeMultipart object to store multiple BodyPart objects

      BodyPart mdp = new MimeBod

      yPart();//Create a new BodyPart object to store the content of the letter

      mdp.setContent(content,"text/html;charset=gb2312");

      mul.addBodyPart(mdp );//Add the BodyPart containing the content of the letter to the MimeMultipart object

      //Set the attachment of the letter

      mdp = new MimeBodyPart();//Create a new BodyPart to store the attachment

      DataHandler handler = new DataHandler("JAVAMAIL attachment test","text/plain;charset=gb2312");//Create a new DataHandler object and set its content, format and encoding method

      mdp.setFileName("mrsoft");//can be inconsistent with the source file name

      mdp.setDataHandler(handler);

      mul.addBodyPart(mdp);

      p>

      message.setContent(mul);//Use mul as the content of the message object

      message.saveChanges();

      Transport transport = sess.getTransport("smtp ");

      transport.connect("smtp."+server,from_mail,from_psd);

      transport.sendMessage(message,message.getAllRecipients());

      transport.close();

      out.println("Attachment sent successfully!");

      }catch(Exception ex){

      out .print("The email failed to be sent, the reason may be:

        ");

        out.println(ex.getMessage());

        }

        %>

        4. Mass mail

        Set the recipient's address in the form of tomail+i, and use a For loop to send mail to these addresses to achieve the purpose of mass mail.

        Use the Address class to set the recipient and sender information of the email message. After creating the email address class, set the sender of the email through the setFrom() method of message. The code is as follows:

        p>

        message.setFrom(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 3 constants to distinguish the type of recipient:

        1) Message.RecipientType.TO--Send.

        2) Message.RecipientType.CC--CC.

        3) Message.RecipientType.BCC--Blank delivery.

        Note: In daily use, two forms of email sending, CC and BCC, are often used. Although CC and BCC both send an email to multiple mailboxes at the same time, there are certain differences between them. The difference is that BCC hides the names of other CC's. Address, only the recipient can see their own receiving address, which can play a role in confidentiality and preventing spam.

        Implementation process:

        <%@ page import="java.io.*"%>

        <%@ page import="java.util. *"%>

        <%@ page import="javax.mail.*"%>

        <%@ page import="javax.mail.internet.*"%>

        <%@ page import="javax.activation.*"%>

        <%

        try{

        int i = 1;

        for(i=1;i<4;i++){

        request.setCharacterEncoding("gb2312");

        String from = request. getParameter("from_mail");

        String to = request.getParameter("tomail"+i);

        String subject = request.getParameter("subject");

        String messageText = request.getParameter("message");

        String password = request.getParameter("password");

        String S = request.getParameter(" from_mail");

        int n = S.indexOf('@');

        int m = S.length();

        String server = S .substring(n+1,m);

        //Create a mail session

        Properties pro = new Properties();

        pro.put("mail .smtp.host","smtp."+server);

        pro.put("mail.smtp.auth","true");

        Session sess = Session. getInstance(pro);

        sess.setDebug(true);

        MimeMessage message = new MimeMessage(sess);//Create a new message object

        InternetAddress from_mail = new InternetAddress(from);//Set the sender

        message.setFrom(from_mail);

        //Set the recipient

        InternetAddress to = new InternetAddress(to);

        message.setRecipient(Message.RecipientType.TO,to_mail);

        message.setSubject(subject);//Set the subject

        message.setText(messageText);//Set content

        message.setSentDate(new Date());//Set sending time

        message.saveChanges();//Save Email information

        > Transport transport = sess.getTransport("smtp");

        transport.connect("smtp."+server,from,password);

        transport.sendMessage(message,message .getAllRecipients());

        transport.close();

        if(i==3){

        out.print("Sent email successfully" );

        }

        }

        }catch(Exception e){

        out.print("Failed to send email, possible reasons Yes

          ");

          out.println(e.getMessage());

          }

          %>

          5 .Spring uses WebService to send mobile phone text messages

          Sending short messages through programming is a relatively cumbersome matter. The current general solution is to connect the computer and mobile phone and use mobile phone programming language to write relevant SMS program, and this method not only requires time and energy, but also requires corresponding hardware facilities, and you must know the relevant knowledge of mobile phone programming. You can remotely access the existing SMS sending server through Web Service technology, and send SMS messages by calling the server's method.

          Technical points:

          Use existing resources: a Web Service remote service that can send text messages. This Web Service is provided by Sina website, which allows users to directly call remote services to send messages. The service name of this service is SMSWS, and the service port is SMSWebServiceSoapPort. This remote service provides a method sendXml() for sending short messages. The definition of this method is as follows:

          public String sendXml(String carrier,String id,String password,String toMobile,String message,String msgtype)

          Function: According to the parameters given by the user Send a text message to the target user's mobile phone.

          Parameter description:

          The six parameters in the sendXml() method are all of String type, and the return value of the sendXml() method is also of String type. The following is a detailed description of the six parameters in the sendXml() method.

          1) Carrier: Carrier name. There are no specific requirements for this parameter when used. That is, you can enter it casually, and the entered string will not be displayed on the other party's mobile phone.

          2) id: mobile phone number registered on Sina.com. When using Sina services to send mobile text messages, you need to register your mobile phone on the Sina website. The registration URL is:.

          3) Password: The password fed back by Sina after successfully registering the mobile phone on Sina.

          4) toMobile: The other party’s mobile phone number to receive text messages.

          5) message: the content of the short message to be sent.

          6) msgtype: The type of short message sent. Since it is not a multimedia message, enter Text.

          To call the sendXml() method, you need to configure an instance of JaxRpcPortProxyFactoryBean in Spring, and then define the interface containing this method. Spring will automatically obtain the interface of the remote service.

          Implementation process:

          ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

          SMSWebService sendSms = (SMSWebService)ac.getBean("helloWorldService" );

          String carrier = "lzwsky";

          String id = userid.getText();

          String password = passwordField.getText();

          p>

          String toMobile = toMobileField.getText();

          String message = messageField.getText();

          String msgtype = "Text";

          sendSms.sendXml(carrier,id,password,toMobile,message,msgtype);