| 点点头 |
 |
| 等级:社区游侠 |
| 权限:普通用户 |
| 积分:25 |
| 金钱:3308 |
| 声望:52 |
| 经验:52 |
| 发帖数:775 |
| 注册:2007年1月16日 |
|
|
|
|
- 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;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class SendMail {
- private static String username = "xxxx";
- private static String password = "xxxx";
- private static String smtpServer = "smtp.163.com";
- private static String fromMailAddress = "xxxx@163.com";
- private static String toMailAddress = "sunyujia@yahoo.cn";
-
- 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));
-
-
- MimeMessage mimeMessage = new MimeMessage(session);
- mimeMessage.setFrom(new InternetAddress(fromMailAddress));
- mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
- toMailAddress));
- mimeMessage.setSubject("主题");
- mimeMessage.setSentDate(new Date());
- Multipart mp = new MimeMultipart("related");
-
- BodyPart bodyPart = new MimeBodyPart();
- bodyPart.setDataHandler(new DataHandler("测<img src="cid:IMG1" />试",
- "text/html;charset=GBK"));
-
- 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>");
- mp.addBodyPart(imgBodyPart);
-
- mp.addBodyPart(bodyPart);
- mimeMessage.setContent(mp);
- Transport.send(mimeMessage);
-
- }
-
-
-
-
-
-
-
-
- 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();
- }
- }
-
-
-
-
- class SmtpAuthenticator extends Authenticator {
- String username = null;
- String password = null;
-
-
- public SmtpAuthenticator(String username, String password) {
- this.username = username;
- this.password = password;
- }
-
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(this.username, this.password);
- }
-
- }
|
|
|
|
|
|