Java send mail using JAVA QQ-mail sent summary

One, two jar package

Second, the code

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
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;

import com.sun.mail.util.MailSSLSocketFactory;

public class Test1 {
    public static void main(String[] args) {
        sendMail();
    }

    public static void sendMail() {
        String from = "[email protected]";
        String to = "[email protected]";
        String host = "smtp.qq.com";
        try {
            Properties properties = System.getProperties();
            //SSL加密
            SF = MailSSLSocketFactory new new MailSSLSocketFactory (); 
            sf.setTrustAllHosts ( to true ); 
            properties.put ( "mail.smtp.ssl.enable", "to true" ); 
            properties.put ( "mail.smtp.ssl.socketFactory" , SF) ; 
            properties.setProperty ( "mail.smtp.host" , Host); 
            properties.put ( "mail.smtp.auth", "to true" ); 

            // get the session to send a message, access a third-party login authorization code 
            session session = session .getDefaultInstance (Properties, new new the Authenticator () { 
                @Override 
                protectedThe getPasswordAuthentication PasswordAuthentication () {
                     return  new new PasswordAuthentication (from, "XXXXXXXX"); // Sign in authorization code 
                } 
            }); 

            the Message Message = new new the MimeMessage (the session); 

            // prevents the message being spam handling course, put the Outlook vest 
            message.addHeader ( "the X--Mailer", "in the Microsoft Outlook Express 6.00.2900.2869" ); 

            message.setFrom ( new new InternetAddress (from)); 

            message.addRecipient (Message.RecipientType.TO, new new InternetAddress (to)); 

            the Message .setSubject ( "This IS at The Subject Line!");

            BodyPart bodyPart = new MimeBodyPart();

            bodyPart.setText("发送");

            Multipart multipart = new MimeMultipart();

            multipart.addBodyPart(bodyPart);

            //附件
            // bodyPart = new MimeBodyPart();
            // String fileName = "文件路径";
            // DataSource dataSource = new FileDataSource(fileName);
            // bodyPart.setDataHandler(new DataHandler(dataSource));
            // bodyPart.setFileName("文件显示的名称");
            // multipart.addBodyPart(bodyPart);

            message.setContent(multipart);

            Transport.send(message);
            System.out.println("mail transports successfully");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

JAVA QQ use to send e-mail summary

Guess you like

Origin www.cnblogs.com/kikyoqiang/p/12104930.html