使用Java傳送各種格式的郵件
轉自:https://blog.csdn.net/u011031689/article/details/51326595
測試可用:
有些重複程式碼沒有給註釋。類的方法作用自行檢視API瞭解,最後附上原始碼。
首先使用JavaMail的jar,官網可下載。
公共靜態的常量:
-
public final static String MAIL = "@sina.com"; // 郵箱格式
-
public final static String SEND_HOST = "smtp.sina.com"; // 郵箱傳送伺服器
-
public final static String ACCEPT_HOST = "pop.sina.com"; // 郵箱伺服器
-
public final static String SEND_USER = "xxxx"; // 使用者名稱
-
public final static String SEND_PWD = "xxxxx"; // 密碼
-
public final static String FROM_MAIL = SEND_USER + "@sina.com";// 傳送方郵箱地址
-
public final static String TO_MAIL = "[email protected]"; // 接收方郵箱地址
-
public final static String CC_MAIL = SEND_USER + MAIL; // 抄送方郵箱地址
-
public final static String BCC_MAIl = SEND_USER + MAIL; // 密送方郵箱地址
-
public final static String ENCODE = "UTF-8";
-
public static Date date = new Date();
使用java自帶的介面和類傳送文字格式郵件
-
Properties prop = new Properties();
-
prop.setProperty("mail.host", SEND_HOST);
-
prop.setProperty("mail.transport.protocol", "smtp");
-
prop.setProperty("mail.smtps.ssl.enable", "true");
-
// prop.setProperty("mail.smtp.port", "25");
-
prop.setProperty("mail.smtp.auth", "true");
-
Session session = Session.getInstance(prop); //建立應用會話
-
Message message = new MimeMessage(session); //訊息摘要,是郵件的主體
-
message.setSubject("測試"); //設定主題
-
message.setText("你好!"); //郵件內容
-
message.setSentDate(new Date()); //傳送日期
-
message.setFrom(new InternetAddress(FROM_MAIL)); //傳送方
-
message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_MAIL)); //接受方
-
message.saveChanges(); //儲存郵件主體物件內容
-
Transport transport = session.getTransport(); //傳輸物件
-
transport.connect(SEND_HOST, FROM_MAIL, SEND_PWD); //連線伺服器中的郵箱
-
transport.sendMessage(message, message.getAllRecipients()); //傳送
-
transport.close(); //關閉傳輸
-
System.out.println("Successfully send mail to all user");
感覺自帶的介面方法比較麻煩,使用spring封裝的javamail,記的匯入spring相關包。
使用spring傳送文字格式的郵件,程式碼如下:
-
public static void sendTxtMail() throws MailException {
-
JavaMailSenderImpl send = new JavaMailSenderImpl();
-
Properties prop = new Properties();
-
prop.setProperty("mail.transport.protocol", "smtp");
-
prop.setProperty("mail.host", SEND_HOST);
-
prop.setProperty("mail.smtps.ssl.enable", "true");
-
prop.setProperty("mail.smtp.auth", "true");
-
send.setUsername(SEND_USER);
-
send.setPassword(SEND_PWD);
-
send.setJavaMailProperties(prop);
-
SimpleMailMessage msg = new SimpleMailMessage();
-
msg.setFrom(FROM_MAIL);
-
msg.setTo(TO_MAIL);
-
msg.setReplyTo(FROM_MAIL);
-
msg.setCc(CC_MAIL);
-
msg.setBcc(BCC_MAIl);
-
msg.setSentDate(date);
-
msg.setSubject("傳送的文字格式郵件");
-
msg.setText("文字格式 測試成功!");
-
send.send(msg);
-
System.out.println("Successfully send mail to the user");
使用spring的封裝方法傳送Html格式郵件 ,程式碼如下:
-
// 傳送Html格式郵件
-
public static void sendHtmlMail() throws Exception {
-
JavaMailSenderImpl send = new JavaMailSenderImpl();
-
Properties prop = new Properties();
-
prop.setProperty("mail.transport.protocol", "smtp");
-
prop.setProperty("mail.host", SEND_HOST);
-
prop.setProperty("mail.smtps.ssl.enable", "true");
-
prop.setProperty("mail.smtp.auth", "true");
-
send.setUsername(SEND_USER);
-
send.setPassword(SEND_PWD);
-
send.setJavaMailProperties(prop);
-
MimeMessage msg = send.createMimeMessage();
-
MimeMessageHelper helper = new MimeMessageHelper(msg, ENCODE);
-
helper.setFrom(FROM_MAIL);
-
helper.setTo(TO_MAIL);
-
helper.setReplyTo(FROM_MAIL);
-
helper.setCc(CC_MAIL);
-
helper.setBcc(BCC_MAIl);
-
helper.setSentDate(date);
-
helper.setSubject("傳送的HTML格式郵件");
-
String html = "<font size='5' color='red'>HTML格式測試成功!</font>";
-
helper.setText(html, true); // 郵件內容,引數true表示是html程式碼
-
send.send(msg);
-
System.out.println("Successfully send mail to the user");
使用spring的封裝方法傳送帶內嵌內容的Html格式郵件 ,程式碼如下:
-
// 傳送帶內嵌檔案的HTML格式郵件
-
public static void sendInlineMail() throws Exception {
-
// spring提供的郵件實現類
-
JavaMailSenderImpl send = new JavaMailSenderImpl();
-
Properties prop = new Properties();
-
prop.setProperty("mail.transport.protocol", "smtp"); // 設定郵件傳送協議
-
prop.setProperty("mail.host", SEND_HOST); // 郵件伺服器地址
-
prop.setProperty("mail.smtps.ssl.enable", "true"); // 郵件ssl驗證
-
prop.setProperty("mail.smtp.auth", "true"); // 郵件服務身份驗證
-
send.setUsername(SEND_USER); // 設定使用者名稱
-
send.setPassword(SEND_PWD); // 設定密碼
-
send.setJavaMailProperties(prop);
-
MimeMessage msg = send.createMimeMessage();
-
// 指定HTML編碼,引數true表示為multipart
-
MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODE);
-
helper.setFrom(FROM_MAIL); // 傳送者郵箱
-
helper.setTo(TO_MAIL); // 接收者郵箱
-
helper.setReplyTo(FROM_MAIL); // 回覆郵箱
-
helper.setCc(CC_MAIL); // 抄送郵箱
-
helper.setBcc(BCC_MAIl); // 密送郵箱
-
helper.setSentDate(date); // 傳送日期
-
helper.setSubject("傳送的帶有內部檔案的HTML格式郵件");
-
String html = "<font size='5' color='red'>HTML格式測試成功!</font>"
-
+ "<img src ='cid:demoimg'>"; // cid是一個固定字首,demoimg是一個資源名稱
-
helper.setText(html, true); // 郵件內容,引數true表示是html程式碼
-
ClassPathResource resource = new ClassPathResource("col.jpg"); // 載入專案路徑下資源
-
helper.addInline("demoimg", resource); // 將資源繫結到demoimg上
-
send.send(msg); // 傳送郵件
-
System.out.println("Successfully send mail to the user");
-
}
使用spring的封裝方法傳送帶附件的郵件 ,程式碼如下:
-
// 傳送帶附件的郵件
-
public static void sendAttachmentMail() throws Exception {
-
// spring提供的郵件實現類
-
JavaMailSenderImpl send = new JavaMailSenderImpl();
-
Properties prop = new Properties();
-
prop.setProperty("mail.transport.protocol", "smtp"); // 設定郵件傳送協議
-
prop.setProperty("mail.host", SEND_HOST); // 郵件伺服器地址
-
prop.setProperty("mail.smtps.ssl.enable", "true"); // 郵件ssl驗證
-
prop.setProperty("mail.smtp.auth", "true"); // 郵件服務身份驗證
-
send.setUsername(SEND_USER); // 設定使用者名稱
-
send.setPassword(SEND_PWD); // 設定密碼
-
send.setJavaMailProperties(prop);
-
MimeMessage msg = send.createMimeMessage();
-
// 指定HTML編碼,引數true表示為multipart
-
MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODE);
-
helper.setFrom(FROM_MAIL); // 傳送者郵箱
-
helper.setTo(TO_MAIL); // 接收者郵箱
-
helper.setReplyTo(FROM_MAIL); // 回覆郵箱
-
helper.setCc(CC_MAIL); // 抄送郵箱
-
helper.setBcc(BCC_MAIl); // 密送郵箱
-
helper.setSentDate(date); // 傳送日期
-
helper.setSubject("傳送帶有附件的郵件");
-
String html = "<font size='5' color='red'>附件測試成功!</font>";
-
helper.setText(html, true); // 郵件內容,引數true表示是html程式碼
-
String demo = "demo.docx";
-
ClassPathResource resource = new ClassPathResource(demo); // 載入專案路徑下資源
-
helper.addAttachment(MimeUtility.encodeWord(demo), resource); // 如果檔案是中文名,需要轉碼。
-
send.send(msg); // 傳送郵件
-
System.out.println("Successfully send mail to the user");
-
}
在測試之前記得將郵箱smtp、pop設定上,否者會報驗證錯誤或連線服務錯誤 即50X系列錯誤。