Java實現QQ郵件發送

首先咱們須要兩個jar包,點擊下面便可下載這兩個包:java

咱們這裏採用QQ郵箱發送郵件爲例,代碼以下:安全

package ddd;

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 SendEmail {

    public static void main(String[] args) {
        try {
            
            //設置發件人
            String from = "xxx@qq.com";
            
            //設置收件人
            String to = "xxxx@qq.com";
            
            //設置郵件發送的服務器,這裏爲QQ郵件服務器
            String host = "smtp.qq.com";
            
            //獲取系統屬性
            Properties properties = System.getProperties();
            
            //SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);
            
            //設置系統屬性
            properties.setProperty("mail.smtp.host", host);
            properties.put("mail.smtp.auth", "true");
            
            //獲取發送郵件會話、獲取第三方登陸受權碼
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, "第三方登陸受權碼");
                }
            });
            
            Message message = new MimeMessage(session);
            
            //防止郵件被固然垃圾郵件處理,披上Outlook的馬甲
            message.addHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.2869");
            
            message.setFrom(new InternetAddress(from));
            
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            
            //郵件標題
            message.setSubject("This is 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();
        }
    }
}

QQ郵箱發送郵件記得要在設置裏面開啓POP3/SMTP服務,而後獲取第三方登陸的受權碼。服務器

上面的代碼中啓用了SSL加密,網上不少人說QQ發送郵件不加上SSL加密會報錯,樓主這裏不加也是能夠發送的不知道爲何,可是爲了數據安全仍是加上了。session

有些人發送的郵件會被當作垃圾郵件處理,這裏我也進行了處理,給郵件頭披上Outlook的馬甲,固然也能夠將郵件內容以HTML格式發送,以防止被當成垃圾郵件。socket

上述就是一個簡單的java發送QQ帶附件的郵件的代碼。ide

相關文章
相關標籤/搜索