java pop3发送邮件_pop3_用Java发送图文并茂的HTML邮件

packagecom.syj;importjava.io.ByteArrayOutputStream;importjava.io.FileInputStream;importjava.io.IOException;importjava.util.Arrays;importjava.util.Date;importjava.util.Properties;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjavax.mail.BodyPart;importjavax.mail.Multipart;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMultipart;importcom.sun.istack.internal.ByteArrayDataSource;/***

* Title:用java发送邮件的例子

*

*

*

* Description:发送图片附件并在html中使用该图片

*

*

*

* Copyright: Copyright (c) 2007

*

** @main sunyujia@yahoo.cn

* @date Jun 10, 2008 12:35:26 AM*/

public classSendMail {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) throwsException {

Properties props= newProperties();

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.host", smtpServer);//获得邮件会话对象

Session session =Session.getDefaultInstance(props,newSmtpAuthenticator(username, password));/******************************************************/

//创建MIME邮件对象

MimeMessage mimeMessage = newMimeMessage(session);

mimeMessage.setFrom(new InternetAddress(fromMailAddress));//发件人

mimeMessage.setRecipient(Message.RecipientType.TO, newInternetAddress(

toMailAddress));//收件人

mimeMessage.setSubject("主题");

mimeMessage.setSentDate(new Date());//发送日期

Multipart mp = new MimeMultipart("related");//related意味着可以发送html格式的邮件

/******************************************************/BodyPart bodyPart= new MimeBodyPart();//正文

bodyPart.setDataHandler(new DataHandler("测试","text/html;charset=GBK"));//网页格式

/******************************************************/BodyPart attachBodyPart= new MimeBodyPart();//普通附件

FileDataSource fds = new FileDataSource("c:/boot.ini");

attachBodyPart.setDataHandler(newDataHandler(fds));

attachBodyPart.setFileName("=?GBK?B?"

+ newsun.misc.BASE64Encoder().encode(fds.getName().getBytes())+ "?=");//解决附件名中文乱码

mp.addBodyPart(attachBodyPart);/******************************************************/MimeBodyPart imgBodyPart= new MimeBodyPart(); //附件图标

byte[] bytes = readFile("C:/button.gif");

ByteArrayDataSource fileds= newByteArrayDataSource(bytes,"application/octet-stream");

imgBodyPart.setDataHandler(newDataHandler(fileds));

imgBodyPart.setFileName("button.gif");

imgBodyPart.setHeader("Content-ID", "");//在html中使用该图片方法src="cid:IMG1"

mp.addBodyPart(imgBodyPart);/******************************************************/mp.addBodyPart(bodyPart);

mimeMessage.setContent(mp);//设置邮件内容对象

Transport.send(mimeMessage);//发送邮件

}/*** 读取文件

*

*@paramfile

* 文件路径

*@return返回二进制数组*/

public static byte[] readFile(String file) {

FileInputStream fis= null;

ByteArrayOutputStream bos= null;try{

fis= newFileInputStream(file);

bos= newByteArrayOutputStream();intbytesRead;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();

}

}returnbos.toByteArray();

}

}/*** Smtp认证*/

class SmtpAuthenticator extendsAuthenticator {

String username= null;

String password= null;//SMTP身份验证

publicSmtpAuthenticator(String username, String password) {this.username =username;this.password =password;

}publicPasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(this.username, this.password);

}

}

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页