`
supben
  • 浏览: 326610 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 发送邮件

阅读更多
以下是两种邮件发送方式。
给出的例子是是发送HTML格式带附件的邮件。(普通文本格式的邮件基本上可以不关心,现在的邮件都是HTML格式的!)
如果不要发送附件,只要发送单纯的HTML邮件。只要把附件部分去掉即可


/**
*用spring mail 发送邮件,依赖jar:spring.jar,activation.jar,mail.jar 
*/

public static void sendFileMail() throws MessagingException {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

		// 设定mail server
		senderImpl.setHost("smtp.126.com");
		senderImpl.setUsername("yuhan0");
		senderImpl.setPassword("******");
		// 建立HTML邮件消息
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		// true表示开始附件模式
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

		// 设置收件人,寄件人
		messageHelper.setTo("slimes@126.com");
		messageHelper.setFrom("yuhan0@126.com");
		messageHelper.setSubject("测试邮件!");
		// true 表示启动HTML格式的邮件
		messageHelper.setText("<html><head></head><body><h1>你好:附件!!</h1></body></html>", true);

		FileSystemResource file1 = new FileSystemResource(new File("d:/logo.jpg"));
		FileSystemResource file2 = new FileSystemResource(new File("d:/读书.txt"));
		// 添加2个附件
		messageHelper.addAttachment("logo.jpg", file1);
		
		try {
			//附件名有中文可能出现乱码
			messageHelper.addAttachment(MimeUtility.encodeWord("读书.txt"), file2);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new MessagingException();
		}
		// 发送邮件
		senderImpl.send(mailMessage);
		System.out.println("邮件发送成功.....");

	}







/**
*用apache commons-email 发送邮件
*依赖jar:commons-email.jar,activation.jar,mail.jar
*/
public static void sendMutiMessage() {

		MultiPartEmail email = new MultiPartEmail();
		String[] multiPaths = new String[] { "D:/1.jpg", "D:/2.txt" };

		List<EmailAttachment> list = new ArrayList<EmailAttachment>();
		for (int j = 0; j < multiPaths.length; j++) {
			EmailAttachment attachment = new EmailAttachment();
			//判断当前这个文件路径是否在本地  如果是:setPath  否则  setURL; 
			if (multiPaths[j].indexOf("http") == -1) {
				attachment.setPath(multiPaths[j]);
			} else {
				try {
					attachment.setURL(new URL(multiPaths[j]));
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
			}
			attachment.setDisposition(EmailAttachment.ATTACHMENT);
			attachment.setDescription("Picture of John");
			list.add(attachment);
		}

		try {
			// 这里是发送服务器的名字:
			email.setHostName("smtp.126.com");
			// 编码集的设置
			email.setCharset("utf-8");
			// 收件人的邮箱				
			email.addTo("slimes@126.com");
			// 发送人的邮箱
			email.setFrom("yuhan0@126.com");
			// 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
			email.setAuthentication("yuhan0", "******");
			email.setSubject("这是一封测试邮件");
			// 要发送的信息
			email.setMsg("<b><a href=\"http://www.baidu.com\">邮件测试内容</a></b>");

			for (int a = 0; a < list.size(); a++) //添加多个附件   
			{
				email.attach(list.get(a));
			}
			// 发送
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}

分享到:
评论
8 楼 wuyizhong 2012-12-29  
用的第一个,很好用哒。 博主太给力。
7 楼 cn-done 2010-11-21  

common email
http://commons.apache.org/email/userguide.html

以前研究java email的时候整理了一篇文档,参照了一些TX的文章,
如有雷同,纯属借鉴,就当为原创们做下宣传。
6 楼 wangyu1221 2010-11-21  
前段时间给公司的系统做过一个javamail的邮件收发系统,发邮件问题不是很大,就算带附件转发也没有特别困难的地方。
反倒是收邮件比较麻烦,特别是oe发过来的。还有百度发过来的标题乱码……

总的说来邮件收发细节很多,很繁琐。
5 楼 supben 2010-11-19  
Lucky_Man 写道
太给力了,邮箱名、密码都写出来


那又怎么了
4 楼 supben 2010-11-19  
manchester1878 写道
第一个例子报错
com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required



很明显是认证信息输入错误了。
有的邮箱要完整后缀,有的只需要@前的部分。
3 楼 Lucky_Man 2010-11-19  
太给力了,邮箱名、密码都写出来
2 楼 manchester1878 2010-11-18  
第一个例子报错
com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required
1 楼 kyan54 2010-11-18  
测试。你的第2个不是HTML格式的。。

相关推荐

Global site tag (gtag.js) - Google Analytics