Java實現QQ郵件發送


首先我們需要兩個jar包,點擊下面即可下載這兩個包:

我們這里采用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加密會報錯,樓主這里不加也是可以發送的不知道為什么,但是為了數據安全還是加上了。

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM