用Java發(fā)送圖文并茂的HTML郵件_.Net教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:用在JavaScript的RequestHelper碰到一個小小的需求,就是要根據(jù)傳入的錨(也就是url中#后面的東西啦)來顯示不同的內(nèi)容,記得以前寫了的,不知道被我丟到哪去了,又要重新寫一個,順便把功能整理加強了一些,加入了取QueryString
- package com.syj;
- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.mail.BodyPart;
- import javax.mail.Multipart;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMultipart;
- import com.sun.istack.internal.ByteArrayDataSource;
- /**
- * <P>
- * Title:用java發(fā)送郵件的例子
- * </P>
- *
- * <P>
- * Description:發(fā)送圖片附件并在html中使用該圖片
- * </P>
- *
- * <P>
- * Copyright: Copyright (c) 2007
- * </P>
- *
- * @author 孫鈺佳
- * @main [email protected]
- * @date Jun 10, 2008 12:35:26 AM
- */
- public class SendMail {
- private static String username = "xxxx";
- private static String password = "xxxx";
- private static String smtpServer = "smtp.163.com";
- private static String fromMailAddress = "[email protected]";
- private static String toMailAddress = "[email protected]";
- public static void main(String[] args) throws Exception {
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.host", smtpServer);
- // 獲得郵件會話對象
- Session session = Session.getDefaultInstance(props,
- new SmtpAuthenticator(username, password));
- /** *************************************************** */
- // 創(chuàng)建MIME郵件對象
- MimeMessage mimeMessage = new MimeMessage(session);
- mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 發(fā)件人
- mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
- toMailAddress));// 收件人
- mimeMessage.setSubject("主題");
- mimeMessage.setSentDate(new Date());// 發(fā)送日期
- Multipart mp = new MimeMultipart("related");// related意味著可以發(fā)送html格式的郵件
- /** *************************************************** */
- BodyPart bodyPart = new MimeBodyPart();// 正文
- bodyPart.setDataHandler(new DataHandler("測<img src="cid:IMG1" />試",
- "text/html;charset=GBK"));// 網(wǎng)頁格式
- /** *************************************************** */
- BodyPart attachBodyPart = new MimeBodyPart();// 普通附件
- FileDataSource fds = new FileDataSource("c:/boot.ini");
- attachBodyPart.setDataHandler(new DataHandler(fds));
- attachBodyPart.setFileName("=?GBK?B?"
- new sun.misc.BASE64Encoder().encode(fds.getName().getBytes())
- "?=");// 解決附件名中文亂碼
- mp.addBodyPart(attachBodyPart);
- /** *************************************************** */
- MimeBodyPart imgBodyPart = new MimeBodyPart(); // 附件圖標
- byte[] bytes = readFile("C:/button.gif");
- ByteArrayDataSource fileds = new ByteArrayDataSource(bytes,
- "application/octet-stream");
- imgBodyPart.setDataHandler(new DataHandler(fileds));
- imgBodyPart.setFileName("button.gif");
- imgBodyPart.setHeader("Content-ID", "<IMG1></IMG1>");// 在html中使用該圖片方法src="cid:IMG1"
- mp.addBodyPart(imgBodyPart);
- /** *************************************************** */
- mp.addBodyPart(bodyPart);
- mimeMessage.setContent(mp);// 設(shè)置郵件內(nèi)容對象
- Transport.send(mimeMessage);// 發(fā)送郵件
- }
- /**
- * 讀取文件
- *
- * @param file
- * 文件路徑
- * @return 返回二進制數(shù)組
- */
- public static byte[] readFile(String file) {
- FileInputStream fis = null;
- ByteArrayOutputStream bos = null;
- try {
- fis = new FileInputStream(file);
- bos = new ByteArrayOutputStream();
- int bytesRead;
- byte buffer[] = new byte[1024 * 1024];
- while ((bytesRead = fis.read(buffer)) != -1) {
- bos.write(buffer, 0, bytesRead);
- Arrays.fill(buffer, (byte) 0);
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- } finally {
- try {
- if (bos != null)
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return bos.toByteArray();
- }
- }
- /**
- * Smtp認證
- */
- class SmtpAuthenticator extends Authenticator {
- String username = null;
- String password = null;
- // SMTP身份驗證
- public SmtpAuthenticator(String username, String password) {
- this.username = username;
- this.password = password;
- }
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(this.username, this.password);
- }
- }
分享:ASP.NET2.0中控件的簡單異步回調(diào)雖然已經(jīng)有了ASP.NET AJAX了,最近學(xué)習(xí)ASP.NET控件的時候,逐步理解了原始的控件異步回調(diào)(代碼取自《ASP.NET 2.0 高級編程》): 首先,在Render事件中添加好一個事件
相關(guān).Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實例(可帶附件)
- js實現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實現(xiàn)
- Asp.Net 無刷新文件上傳并顯示進度條的實現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- asp.net頁面?zhèn)髦禍y試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現(xiàn)分頁示例代碼
- 相關(guān)鏈接:
- 教程說明:
.Net教程-用Java發(fā)送圖文并茂的HTML郵件
。