Java中利用qqemai发送QQ邮件
生活随笔
收集整理的這篇文章主要介紹了
Java中利用qqemai发送QQ邮件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近自己了解下Java中利用mai發(fā)送QQ郵件
1.QQ郵箱設置
1.1 進去QQ郵箱-->設置-->賬號-->進行設置如下圖
以下源碼
package test;
import java.io.UnsupportedEncodingException; ?import java.util.Date; ?
import java.util.Properties; ?
??
import javax.activation.DataHandler; ?
import javax.activation.FileDataSource; ?
import javax.mail.Authenticator; ?
import javax.mail.BodyPart; ?
import javax.mail.Message.RecipientType; ?
import javax.mail.MessagingException; ?
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 javax.mail.internet.MimeUtility; ?
??
/**?
?* 發(fā)送郵件的測試程序(適用qq郵箱)?
?* 通過本人的qq郵箱: xxx@qq.com 發(fā)送郵件?
?* @author fcl?
?* ?
?*/ ?
public class MailTest { ?
??
? ? //發(fā)送的郵箱 內部代碼只適用qq郵箱 ?
? ? private static final String USER = "XXXX@qq.com"; ?
? ? //授權密碼 通過QQ郵箱設置->賬戶->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務->開啟POP3/SMTP服務獲取 ?
? ? private static final String PWD = "qazmkhvsaniozgibiffzaq"; ?
? ? ??
? ? private String[] to; ?
? ? private String[] cc;//抄送 ?
? ? private String[] bcc;//密送 ?
? ? private String[] fileList;//附件 ?
? ? private String subject;//主題 ?
? ? private String content;//內容,可以用html語言寫 ?
? ? public void sendMessage() throws MessagingException, UnsupportedEncodingException { ?
? ? ? ? // 配置發(fā)送郵件的環(huán)境屬性 ?
? ? ? ? final Properties props = new Properties(); ?
? ? ? ? //下面兩段代碼是設置ssl和端口,不設置發(fā)送不出去。 ?
? ? ? ? props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); ?
? ? ? ? //props.setProperty("mail.smtp.port", "465"); ?
? ? ? ? props.setProperty("mail.smtp.socketFactory.port", "465"); ?
? ? ? ? // 表示SMTP發(fā)送郵件,需要進行身份驗證 ?
? ? ? ? props.setProperty("mail.transport.protocol", "smtp");// 設置傳輸協(xié)議 ?
? ? ? ? props.put("mail.smtp.auth", "true"); ?
? ? ? ? props.put("mail.smtp.host", "smtp.qq.com");//QQ郵箱的服務器 如果是企業(yè)郵箱或者其他郵箱得更換該服務器地址 ?
? ? ? ? // 發(fā)件人的賬號 ?
? ? ? ? props.put("mail.user", USER); ?
? ? ? ? // 訪問SMTP服務時需要提供的密碼 ??
? ? ? ? props.put("mail.password", PWD); ?
??
? ? ? ? // 構建授權信息,用于進行SMTP進行身份驗證 ?
? ? ? ? Authenticator authenticator = new Authenticator() { ?
? ? ? ? ? ? @Override ?
? ? ? ? ? ? protected PasswordAuthentication getPasswordAuthentication() { ?
? ? ? ? ? ? ? ? // 用戶名、密碼 ?
? ? ? ? ? ? ? ? String userName = props.getProperty("mail.user"); ?
? ? ? ? ? ? ? ? String password = props.getProperty("mail.password"); ?
? ? ? ? ? ? ? ? return new PasswordAuthentication(userName, password); ?
? ? ? ? ? ? } ?
? ? ? ? }; ?
? ? ? ? // 使用環(huán)境屬性和授權信息,創(chuàng)建郵件會話 ?
? ? ? ? Session mailSession = Session.getInstance(props, authenticator); ?
? ? ? ? // 創(chuàng)建郵件消息 ?
? ? ? ? MimeMessage message = new MimeMessage(mailSession); ?
? ? ? ? BodyPart messageBodyPart = new MimeBodyPart(); ??
? ? ? ? Multipart multipart = new MimeMultipart(); ??
? ? ? ? // 設置發(fā)件人 ?
? ? ? ? InternetAddress form = new InternetAddress( ?
? ? ? ? ? ? ? ? props.getProperty("mail.user")); ?
? ? ? ? message.setFrom(form); ?
? ? ? ? //發(fā)送 ?
? ? ? ? if (to != null) { ??
? ? ? ? ? ? String toList = getMailList(to); ??
? ? ? ? ? ? InternetAddress[] iaToList = new InternetAddress().parse(toList); ??
? ? ? ? ? ? message.setRecipients(RecipientType.TO, iaToList); // 收件人 ??
? ? ? ? } ??
? ? ? ? //抄送 ??
? ? ? ? if (cc != null) { ??
? ? ? ? ? ? String toListcc = getMailList(cc); ??
? ? ? ? ? ? InternetAddress[] iaToListcc = new InternetAddress().parse(toListcc); ??
? ? ? ? ? ? message.setRecipients(RecipientType.CC, iaToListcc); // 抄送人 ??
? ? ? ? } ??
? ? ? ? //密送 ??
? ? ? ? if (bcc != null) { ??
? ? ? ? ? ? String toListbcc = getMailList(bcc); ??
? ? ? ? ? ? InternetAddress[] iaToListbcc = new InternetAddress().parse(toListbcc); ??
? ? ? ? ? ? message.setRecipients(RecipientType.BCC, iaToListbcc); // 密送人 ??
? ? ? ? } ??
? ? ? ? message.setSentDate(new Date()); // 發(fā)送日期 該日期可以隨意寫,你可以寫上昨天的日期(效果很特別,親測,有興趣可以試試),或者抽象出來形成一個參數(shù)。 ?
? ? ? ? message.setSubject(subject); // 主題 ??
? ? ? ? message.setText(content); // 內容 ??
? ? ? ? //顯示以html格式的文本內容 ??
? ? ? ? messageBodyPart.setContent(content,"text/html;charset=utf-8"); ??
? ? ? ? multipart.addBodyPart(messageBodyPart); ??
? ? ? ? //保存多個附件 ??
? ? ? ? if(fileList!=null){ ??
? ? ? ? ? ? addTach(fileList, multipart); ??
? ? ? ? } ??
? ? ? ? message.setContent(multipart); ??
? ? ? ? // 發(fā)送郵件 ?
? ? ? ? Transport.send(message); ?
? ? } ?
??
? ? public void setTo(String[] to) { ?
? ? ? ? this.to = to; ?
? ? } ?
??
? ? public void setCc(String[] cc) { ?
? ? ? ? this.cc = cc; ?
? ? } ?
??
? ? public void setBcc(String[] bcc) { ?
? ? ? ? this.bcc = bcc; ?
? ? } ?
? ? ??
? ? public void setSubject(String subject) { ?
? ? ? ? this.subject = subject; ?
? ? } ?
? ? ??
? ? public void setContent(String content) { ?
? ? ? ? this.content = content; ?
? ? } ?
? ? ??
? ? public void setFileList(String[] fileList) { ?
? ? ? ? this.fileList = fileList; ?
? ? } ?
? ? ??
? ? private String getMailList(String[] mailArray) { ??
? ? ? ? StringBuffer toList = new StringBuffer(); ??
? ? ? ? int length = mailArray.length; ??
? ? ? ? if (mailArray != null && length < 2) { ??
? ? ? ? ? ? toList.append(mailArray[0]); ??
? ? ? ? } else { ??
? ? ? ? ? ? for (int i = 0; i < length; i++) { ??
? ? ? ? ? ? ? ? toList.append(mailArray[i]); ??
? ? ? ? ? ? ? ? if (i != (length - 1)) { ??
? ? ? ? ? ? ? ? ? ? toList.append(","); ??
? ? ? ? ? ? ? ? } ??
? ? ? ? ? ? } ??
? ? ? ? } ??
? ? ? ? return toList.toString(); ??
? ? } ??
? ? ??
? ? //添加多個附件 ??
? ? public void addTach(String fileList[], Multipart multipart) throws MessagingException, UnsupportedEncodingException { ??
? ? ? ? for (int index = 0; index < fileList.length; index++) { ??
? ? ? ? ? ? ?MimeBodyPart mailArchieve = new MimeBodyPart(); ??
? ? ? ? ? ? ?FileDataSource fds = new FileDataSource(fileList[index]); ??
? ? ? ? ? ? ?mailArchieve.setDataHandler(new DataHandler(fds)); ??
? ? ? ? ? ? ?mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),"UTF-8","B")); ??
? ? ? ? ? ? ?multipart.addBodyPart(mailArchieve); ??
? ? ? ? } ? ??
? ? } ?
? ? ??
? ? //以下是演示demo ?
? ? public static void main(String args[]) { ?
? ? ? ? MailTest mail = new MailTest(); ?
? ? ? ? mail.setSubject("標題XXX"); ?
? ? ? ? mail.setContent("內容XXX"); ?
? ? ? ? //收件人 可以發(fā)給其他郵箱(163等) 下同 ?
? ? ? ? mail.setTo(new String[] {"XXX@qq.com","XXX@qq.com"}); ?
? ? ? ? //抄送 ?
? ? ? ? //mail.setCc(new String[] {"xxx@qq.com","xxx@qq.com"}); ?
? ? ? ? //密送 ?
? ? ? ?// mail.setBcc(new String[] {"xxx@qq.com","xxx@qq.com"}); ?
? ? ? ? //發(fā)送附件列表 可以寫絕對路徑 也可以寫相對路徑(起點是項目根目錄) ?
? ? ? ?// mail.setFileList(new String[] {"file\\附件1.txt","file\\附件2.txt"}); ?
? ? ? ? //發(fā)送郵件 ?
? ? ? ? try { ?
? ? ? ? ? ? mail.sendMessage(); ?
? ? ? ? ? ? System.out.println("發(fā)送郵件成功!"); ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? System.out.println("發(fā)送郵件失敗!"); ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? } ?
}
總結
以上是生活随笔為你收集整理的Java中利用qqemai发送QQ邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ipv6服务器有什么优势,ipv6是什么
- 下一篇: Java浮点数运算