Java邮件发送-亚马逊Simple EMail Service (SES)作SMTP服务器

AWS SES使用介绍可见:https://docs.aws.amazon.com/zh_cn/ses/latest/DeveloperGuide/Welcome.html

总结为一下两点即可:

1. 登陆AWS进入控制台,然后点击SMTP Settings,创建SMTP账户:Create My SMTP Credentials,按提示操作生成username and password,这个看起来类似IAM User的key。

AWS控制台地址为:https://console.aws.amazon.com

2.验证发送邮件地址,另外如果发给其他人,也需要在这里验证,系统会发邮件到对方邮箱让他确认,这样对方才能收到SES发来的邮件,就跟我们订阅新闻邮件类似。

开发 :直接使用文档中得demo即可,这里我选用此demo,也可集成AWS的。

需要下载javax.mail.jar,代码中改换的参数换掉。

 

附加我的代码(加了图片的),部分代码为其他引用:

	public void CreateMessageIn() throws MessagingException, IOException {
		Tools tls = new Tools();		
        //String subject = "Important feedback";
		
		// ****************************创建会话***************************************
		final Properties props = new Properties();

		String emlDestUp = ldest.toUpperCase();
//		if (emlDestUp.contains("@QQ.COM") || emlDestUp.contains("@PIONEER.NET.AU")  ) {
		if (emlDestUp.contains("@QQ.COM")) {
			props.put("mail.smtp.host", "smtp.mxhichina.com");// 发件人使用发邮件的电子信箱服务器
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.user", "service@****.com");
			props.put("mail.password", "P***123456");
			props.put("mail.smtp.port", "25");									
		} else {
			props.put("mail.smtp.host", "email-smtp.us-east-1.amazonaws.com");// 发件人使用发邮件的电子信箱服务器
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.port", "25");
			props.put("mail.user", "AKIAJPEOFR*10*");
			props.put("mail.password", "AuhmFfe1lMRSVxPpo44ZfQjjPGqyiZsymz*10*");
		}
	
		/*
		 * http://blog.csdn.net/u013076997/article/details/53760828?locationNum=14&fps=1
		 * javax.mail发送邮件(带附件)
		 * http://blog.csdn.net/wangxinqn/article/details/1708705
		 * 
		近日使用javamail 为公司的软件添加了邮件收发功能。遇到了Unsupported record version Unknown-50.49异常。

		该异常只会在发送邮件的时候产生,而且是应为所有邮箱使用了SSL加密功能。应该是javamail包的问题

		初步的解决方案是在你的发送类里 加上props.put("mail.smtp.quitwait", "false");将该异常屏蔽调		
		*/
		props.put("mail.smtp.quitwait", "false");
		Authenticator atctr = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				String userName = props.getProperty("mail.user");
				String passWord = props.getProperty("mail.password");
				return new PasswordAuthentication(userName, passWord);				
			}
		};
		
		Session mailsession = Session.getInstance(props, atctr); // 获得默认的session对象
		mailsession.setDebug(true);

		// *****************************构造消息**************************************
		MimeMessage msg = new MimeMessage(mailsession);
		
		InternetAddress from = null;
		if (lfrom == null ) {
			Properties pros = new Properties();
			pros.load(this.getClass().getClassLoader()
					.getResourceAsStream("server.properties"));		
			String fel = "service@paby.com";
			if (dfg > 0)
				fel = pros.getProperty("dreamemail");
			
			from = new InternetAddress(fel);			
		} else
			from = new InternetAddress(lfrom);
		
		msg.setFrom(from);   // 发送者email帐号
		msg.setRecipient(Message.RecipientType.TO, new InternetAddress(ldest)); // 设置收件人地址并规定其类型
		if ( Constant.timeUtcFlag )	 // true	
			msg.setSentDate(tls.getUtcDateStrNowDate());
		else
			msg.setSentDate(new Date()); // 设置发信时间		
		msg.setSubject(ltitle); // 设置主题
		msg.setText(lcontent);
		msg.setContent(lcontent, "text/html;charset=UTF-8"); // 设置 正文

		if ( laddonName != null ) {
			/*// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件  
            Multipart multipart = new MimeMultipart();  
            // 设置邮件的文本内容  
            BodyPart contentPart = new MimeBodyPart();   
            contentPart.setText(lcontent);
            multipart.addBodyPart(contentPart);
            // 添加附件  
            BodyPart messageBodyPart = new MimeBodyPart();  
            DataSource source = new FileDataSource(laddonName);            
            // 添加附件的内容  
            messageBodyPart.setDataHandler(new DataHandler(source));
                         
            // 添加附件的标题  
            // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码  
            messageBodyPart.setFileName(MimeUtility.encodeText("Image"));  
            multipart.addBodyPart(messageBodyPart);  
            msg.setContent(multipart); */ 
			
			// 创建邮件正文  
            MimeBodyPart text = new MimeBodyPart();
            text.setContent(lcontent + "<br/><img src='cid:image_id'/>", "text/html;charset=UTF-8");
			// 创建图片  
            MimeBodyPart img = new MimeBodyPart();   
            DataHandler dh = new DataHandler(new FileDataSource(laddonName));//图片路径  
            img.setDataHandler(dh);
            img.setContentID("image_id"); // 创建图片的一个表示用于显示在邮件中显示    

            MimeMultipart mm = new MimeMultipart();  
            mm.addBodyPart(text);  
            mm.addBodyPart(img);  
            mm.setSubType("related");// 设置正文与图片之间的关系  
            // 图片与正文的 body  
            MimeBodyPart all = new MimeBodyPart();  
            all.setContent(mm);
            msg.setContent(mm);
		}
		// 保存邮件  
        msg.saveChanges(); 
        // 发送邮件
		Transport.send(msg);
		logger.info("email has sended to " + ldest);
	}

                ---------------------------------------------欢迎关注公众号(生活不止有代码)-------------------------------------------------------

                                                                      

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值