javamail发送邮件代码

下面是利用sun的javamail框架写的发送邮件代码:

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.URLDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 MailDemo {

	public static void main(String[] args){
		//设置服务器信息
		String mailServerHost = "smtp.163.com";
		String mailServerPort = "25";
		boolean validate = true;
		Properties pro = new Properties();
		pro.put("mail.smtp.host", mailServerHost);
//		pro.put("mai.smtp.port", mailServerPort);
		pro.put("mail.smtp.auth", validate);
		
		//设置邮箱用户名及密码
		final String username = "xxxx@163.com";
		final String password = "xxxx";
		
		Session sendMailSession = Session.getDefaultInstance(pro, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				// TODO Auto-generated method stub
				return new PasswordAuthentication(username, password);
			}
			
		});
		sendMailSession.setDebug(true);
		String from = "xxxx@163.com";
		String to = "xxx@yy.com";
		String subject = "subject";
		String content = "<h1>hello</h1>";
		try {
			Message msg = new MimeMessage(sendMailSession);//创建一个邮件消息
			msg.setFrom(new InternetAddress(from));//创建邮件发送者地址
			msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//创建邮件接受者地址
			msg.setSubject(subject);//设置主题
			msg.setText(content + "1");//设置内容
			msg.setSentDate(new Date());//设置发送日期
			msg.setHeader("X-Mailer", "ligao");
			//发送邮件
			Transport.send(msg);
			
//			Multipart mainPart = new MimeMultipart();
			
//			BodyPart htmlPart = new MimeBodyPart();
//			htmlPart.setContent(content+ "2", "text/html;charset=utf-8");
//			mainPart.addBodyPart(htmlPart);
//			
//			BodyPart attachPart = new MimeBodyPart();
//			((MimeBodyPart) attachPart).attachFile(new File("user.json"));
//			mainPart.addBodyPart(attachPart);
		
//			BodyPart bp = new MimeBodyPart();
//			URLDataSource source = new URLDataSource(new URL("http://www.sina.com"));
//			bp.setFileName("nihao");
//			DataHandler handler = new DataHandler(source);
//			bp.setDataHandler(handler);
//			
//			mainPart.addBodyPart(bp);
			
			//文件源
//			BodyPart bp = new MimeBodyPart();
//			FileDataSource source = new FileDataSource(new File("user.json"));
//			DataHandler handler = new DataHandler(source);
//			bp.setDataHandler(handler);
//			mainPart.addBodyPart(bp);
//			msg.setContent(mainPart);
//		
//			BodyPart bp = new MimeBodyPart();
//			DataHandler handler = new DataHandler("hello", "text/plain;charset=gb2312");
//			bp.setFileName(MimeUtility.encodeWord("你好", "gb2312", null));
//			bp.setDataHandler(handler);
//			mainPart.addBodyPart(bp);
//			msg.setContent(mainPart);
//			msg.setContent(mainPart);
//			Transport.send(msg);
			System.out.println("发送完成");
			
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} 
			
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值