发送邮件代码(含附件)

package com.zpdlut.mail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.Transport;


public class MultipleMail {


public static void main(String[] args) throws Exception{
Properties pros = new Properties();
pros.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(pros);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
MimeMultipart multipart = new MimeMultipart("mixed");
message.setContent(multipart);
message.setSubject("试验一下");
message.setFrom(new InternetAddress(MimeUtility.encodeText("邹鹏")+" <zou-peng315@163.com>"));
message.setReplyTo(new Address[]{new InternetAddress("764109463@qq.com")});
MimeBodyPart attch1 = new  MimeBodyPart();
MimeBodyPart attch2 = new  MimeBodyPart();
MimeBodyPart content = new MimeBodyPart();
multipart.addBodyPart(attch1);
multipart.addBodyPart(attch2);
multipart.addBodyPart(content);
//DataSource ds1 = new FileDataSource(new File("D:\\FindEmail.java"));
//DataHandler dh1 = new DataHandler(ds1 );
// attch1.setDataHandler(dh1);
// attch1.setFileName("Find.java");
attch1.attachFile(new File("D:\\FindEmail.java"));
attch1.setFileName(MimeUtility.encodeText("中国牛.java"));
DataSource ds2 = new FileDataSource(new File("D:\\你好世界.txt"));
DataHandler dh2 = new DataHandler(ds2 );
attch2.setDataHandler(dh2);
attch2.setFileName("helloworld.txt");
MimeMultipart bodyMultipart = new MimeMultipart("related");
content.setContent(bodyMultipart);
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart gifPart = new MimeBodyPart();
bodyMultipart.addBodyPart(htmlPart);
bodyMultipart.addBodyPart(gifPart);
DataSource gifds = new FileDataSource(new File("D:\\logo.jpg"));
DataHandler gifdh = new DataHandler(gifds );
gifPart.setDataHandler(gifdh);

gifPart.setHeader("Content-Location",
"http://hdn.xnimg.cn/photos/hdn511/20090316/10/35/tiny_Fclr_88162l204235.jpg");
htmlPart.setContent("中国要发展,一定得发展 <img src='http://hdn.xnimg.cn/photos/hdn511/20090316/10/35/tiny_Fclr_88162l204235.jpg'> " ,"text/html;charset=gbk");
message.saveChanges();
Transport tran = session.getTransport();
tran.connect("smtp.163.com",25, "zou-peng315@163.com", "-abc8778807");
tran.sendMessage(message, new Address[]{new InternetAddress("zpdlut@gmail.com")});
tran.close();

}

}

转载于:https://www.cnblogs.com/zpdlut/archive/2010/01/30/1660208.html

package gs.gp.Util; import java.util.Properties; import javax.mail.*; import javax.mail.Message.RecipientType; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * 邮件发送工具类 * */ public class MailUtil { /** * 发送邮件方法 * @param to 收件人 * @param code 激活码 */ public static void sendMail(String to,String code){ /** * 1.获得一个Seesion对象 * 2.创建邮件对象(Message) * 3.发送邮件Transport * */ //1.获得连接 Properties props = new Properties();//认证信息 //authenticator 接口信息 Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("huangqian@shop.com", "123456789");//用谁发邮件就是谁 } }); //2.创建邮件对象(Message) Message message = new MimeMessage(session); try { //设置发件人 message.setFrom(new InternetAddress("huangqian@shop.com")); //设置收件人 message.addRecipient(RecipientType.TO, new InternetAddress(to)); //抄送人 cc 密送BCC //设置标题 message.setSubject("来自xxxxxx购物网站商城激活邮件"); //设置邮件正文 message.setContent("<h1>淘宝网站官方激活码!点下面链接进行激活!</h1><h3><a href='http://127.0.0.1:8080/sx/user_active.action?code="+code+"'>www.taobao.com?code="+code+"</a></h3>", "text/html;charset=UTF-8"); //发送邮件 Transport.send(message); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /*public static void main(String[] args) { sendMail("oop@shop.com","222"); }*/ }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值