在使用Java mail之前要先下載Library
下載點:這裡
新增一個class加入以下程式碼,或者把下面程式碼包成jar,完成後將它import進你的程式中
import java.io.File; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class JavaMail { private Properties props = new Properties(); private MimeMessage message; private MimeBodyPart textPart = null; private MimeBodyPart picturePart = null; private MimeBodyPart filePart = null; private Address mailFrom; public JavaMail(String host, String protocol) throws NoSuchProviderException{ this.setProperties(host, protocol); this.createMessage(); } public void setProperties(String host, String protocol) throws NoSuchProviderException{ this.props.setProperty("mail.host", host); this.props.setProperty("mail.transport.protocol", protocol); this.createMessage(); } public void setUserInf(String name, String password) throws NoSuchProviderException{ this.props.setProperty("mail.user", name); this.props.setProperty("mail.password", password); this.createMessage(); } private void createMessage() throws NoSuchProviderException{ Session mailSession = Session.getDefaultInstance(this.props, null); this.message = new MimeMessage(mailSession); } public void setSubject(String subject) throws MessagingException{ this.message.setSubject(subject); } public void setText(String text) throws MessagingException{ this.textPart = new MimeBodyPart(); this.textPart.setContent(text, "text/html; charset=UTF-8"); } public void setPicture(String path, String cid) throws MessagingException{ this.picturePart = new MimeBodyPart(); this.picturePart.setDataHandler(new DataHandler(new FileDataSource(path))); this.picturePart.setFileName(path); this.picturePart.setHeader("Content-ID", cid); } public void setAttFile(String path) throws MessagingException{ if(path != null && path.length() > 0 && new File(path).exists()){ this.filePart = new MimeBodyPart(); this.filePart.setDataHandler(new DataHandler(new FileDataSource(path))); this.filePart.setFileName(path); } } public void setMailFrom(String mailFrom) throws AddressException{ this.mailFrom = new InternetAddress(mailFrom); } public void send(String mailTo) throws MessagingException{ Multipart multipart = new MimeMultipart(); multipart.addBodyPart(this.textPart); if(this.picturePart != null) multipart.addBodyPart(this.picturePart); if(this.filePart != null) multipart.addBodyPart(this.filePart); this.message.setFrom(this.mailFrom); this.message.setContent(multipart); System.out.print("Mail to:" + mailTo + "..."); message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo)); Transport.send(message, message.getRecipients(Message.RecipientType.TO)); System.out.println("OK"); } }
使用方法如下:
JavaMail mail = new JavaMail("mail server host or ip", "mail server protocol"); //如不需要名稱跟密碼,可省略。 mail.setUserInf("name", "password"); //傳送名稱 mail.setMailFrom("mail from"); //信件標題 mail.setSubject("mail title")); //信件內容 mail.setText("xxxxxxxxx"); //附加圖片 mail.setPicture("file path", "Content-ID"); //附加檔案 mail.setAttFile("file path"); //設定收件者Address並傳送 mail.send("mail to");
可在信件內容直接輸入html語法,只要對方有支援就可直接顯示
如果要將圖片加入內容中必需注意content-id的設定
在內容中加入"<img src='cid:image'/>"
cid後面的image必需與setPicture中的Content-ID對應
另外…我只測試過公司的內部Mail Server,沒測過Gmail或Hotmail之類的,有需要的自行測試喔,我有空的話會再另外測試XP
全站熱搜