Java代碼實現發送郵件
import java.io.File;
import java.util.Date;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
-
發送帶附件的郵件
*/
public class AttachmentMailSender {public static boolean sendMail(MailSenderInfo mailInfo) {
MyAuthenticator authenticator = null;
if (mailInfo.isValidate()) {
// 如果需要身份認證,則創建一個密碼驗證器
authenticator = new MyAuthenticator(mailInfo.getUserName(),
mailInfo.getPassword());
}
// 根據郵件發送的屬性和密碼驗證器構造一個發送郵件的session
Session sendMailSession = Session.getInstance(mailInfo
.getProperties(), authenticator);
try {
Message mailMessage = new MimeMessage(sendMailSession);
// 創建郵件發送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 設置郵件消息的發送者
mailMessage.setFrom(from);
// 創建郵件的接收者地址,並設置到郵件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 設置郵件消息的主題
mailMessage.setSubject(mailInfo.getSubject());
// 設置郵件消息發送的時間
mailMessage.setSentDate(new Date());// MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象 Multipart mainPart = new MimeMultipart(); // 創建一個包含HTML內容的MimeBodyPart BodyPart html = new MimeBodyPart(); // 設置HTML內容 html.setContent(mailInfo.getContent(), "text/html; charset=GBK"); mainPart.addBodyPart(html); // 為郵件添加附件 String[] attachFileNames = mailInfo.getAttachFileNames(); if (attachFileNames != null && attachFileNames.length > 0) { // 存放郵件附件的MimeBodyPart MimeBodyPart attachment = null; File file = null; for (int i = 0; i < attachFileNames.length; i++) { attachment = new MimeBodyPart(); // 根據附件文件創建文件數據源 file = new File(attachFileNames[i]); FileDataSource fds = new FileDataSource(file); attachment.setDataHandler(new DataHandler(fds)); // 為附件設置文件名 attachment.setFileName(MimeUtility.encodeWord(file.getName(), "GBK", null)); mainPart.addBodyPart(attachment); } } // 將MiniMultipart對象設置為郵件內容 mailMessage.setContent(mainPart); // 發送郵件 Transport.send(mailMessage); return true; } catch (Exception e) { e.printStackTrace(); return false; }
}
public static void main(String[] args) {
// 創建郵件信息
// 創建郵件信息
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost("mail.XXX.com")//mail.qq.com 這個自己看類型百度查詢下,我們公司地址不透露啦
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("用戶名");
mailInfo.setPassword("密碼");
mailInfo.setFromAddress("你的郵箱");
mailInfo.setToAddress("收件郵箱");
mailInfo.setSubject("MyMail測試");
mailInfo.setContent("我的郵件測試\n\rMy test mail\n\r");
//附件
String[] fileNames = new String[3];
fileNames[0] = "C:/temp/new.txt";
fileNames[1] = "C:/temp/test.wav";
fileNames[2] = "C:/temp/mary_photo.jpg";
mailInfo.setAttachFileNames(fileNames);AttachmentMailSender.sendMail(mailInfo);
}
}
Java代碼實現發送郵件