Spring邮件服务(java邮件)

1、发送邮件配置文件springmail_config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
>
   <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.qq.com" />
        <property name="port" value="25" />
        <property name="username" value="xxxx@qq.com" />
        <property name="password" value="xxxxxxx" />
        <property name="javaMailProperties">
          <props>
           <prop key="mail.smtp.auth">true</prop>
            <!-- 根据情况可进行设置 
            <prop key="mail.smtp.timeout">2500</prop>
           -->
                     </props> 
               </property> 
     </bean> 
</beans>Index of /schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="Index of /schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
>
   <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.qq.com" />
        <property name="port" value="25" />
        <property name="username" value="xxxx@qq.com" />
        <property name="password" value="xxxxxxx" />
        <property name="javaMailProperties">
          <props>
           <prop key="mail.smtp.auth">true</prop>
            <!-- 根据情况可进行设置 
            <prop key="mail.smtp.timeout">2500</prop>
           -->
                     </props> 
               </property> 
     </bean> 
</beans>
2、发送邮件java类
package com.yihongyu.exec.javamail;

import java.io.File;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * SpringMail测试类
 * 
 * @author tzz
 * 
 */
public class SpringMailUtil {
	private ApplicationContext context = null;

	public SpringMailUtil() {
		context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/springmail_config.xml");
	}

	// 简单邮件
	public void simpleSend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");
		SimpleMailMessage mail = new SimpleMailMessage();
		mail.setFrom("xxx@qq.com");
		mail.setTo("xxx@qq.com");
		mail.setSubject(" 测试spring Mail");
		mail.setText("你好,java");
		mailSender.send(mail);
	}

	// 带附件
	public void attachmentSend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");

		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {

			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom("xxx@qq.com");
			helper.setTo("xxx@qq.com");
			helper.setSubject("测试spring Mail附件");
			// 需要将附件显示在html中
			helper.setText("你好,java", true);
			FileSystemResource attachment = new FileSystemResource(new File("E:\\Test2.doc"));
			helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment);// 解决附件中文编码问题

			mailSender.send(mime);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 多附件
	public void moreAttachmentSend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");

		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {

			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom("xxxx@qq.com");
			helper.setTo("xxx@qq.com");
			helper.setSubject("测试spring Mail附件");
			// 需要将附件显示在html中
			helper.setText("你好,java", true);
			FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql"));
			helper.addAttachment("zqt.sql", attachment);
			FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc"));
			helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题

			mailSender.send(mime);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 抄送
	public void copySend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");

		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {

			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom("xxxxx@qq.com");
			helper.setTo("xxxx@qq.com");
			helper.setCc("xxxx@qq.com");
			helper.setSubject("测试spring Mail附件");
			// 需要将附件显示在html中
			helper.setText("你好,java", true);
			FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql"));
			helper.addAttachment("zqt.sql", attachment);
			FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc"));
			helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题

			mailSender.send(mime);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 多附件、多人发送/抄送
	public void moreUserSend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");

		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {

			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom("xxxxx@qq.com");
			helper.setTo("xxxxx@qq.com");// 发送
			// helper.setCc("xxxxxx@qq.com");//抄送
			// helper.setTo(new InternetAddress[] { new InternetAddress("xxxxx@qq.com"),
			// new InternetAddress("xxxx@qq.com") });
			helper.setCc(new InternetAddress[] { new InternetAddress("xxxxxx@qq.com"),
					new InternetAddress("xxxxxx@qq.com") });
			helper.setSubject("测试spring Mail附件");
			// 需要将附件显示在html中
			helper.setText("你好,java", true);
			FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql"));
			helper.addAttachment("zqt.sql", attachment);
			FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc"));
			helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题

			mailSender.send(mime);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		SpringMailUtil springMailUtil = new SpringMailUtil();
		// 简单邮件
		// springMailUtil.simpleSend();
		// 附件
		// springMailUtil.attachmentSend();
		// 多附件
		// springMailUtil.moreAttachmentSend();
		// 抄送
		// springMailUtil.copySend();
		// 多附件、多人发送/抄送
		springMailUtil.moreUserSend();
		System.out.println("发送成功");

	}
}

Java发送Email

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
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;
import javax.mail.internet.MimeUtility;

public class SendEmailUtil {
	
	//发送单个邮箱
	public void sendSingleMail(String emailAddress, String content, String title) {
		String [] emailAddresses = {emailAddress};
		sendGroupMail(emailAddresses, content, title);
	}
	
	// 发送多个邮箱
	public void sendGroupMail(String[] emailAddresses, String content, String title) {
		String emailAddressFrom = "123456@qq.com";//邮箱地址
		String emailServeiHost = "123456@qq.com";//邮箱地址
		final String username = "test";//用户名
		final String password ="test";//密码
		String nickname = "测试账号";//发件人昵称
		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", emailServeiHost);
			props.put("mail.smtp.auth", "true");
			Session session = Session.getDefaultInstance(props,
					new Authenticator() {
				public PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username,password);
				}
			});
			
			MimeMessage mimeMessage = new MimeMessage(session);
			
			mimeMessage.setFrom(new InternetAddress(MimeUtility.encodeText(nickname)+" <"+emailAddressFrom+">"));
			InternetAddress[] emailAddressTo = new InternetAddress[emailAddresses.length];
			for (int i = 0; i < emailAddresses.length; i++) {
				emailAddressTo[i] = new InternetAddress(emailAddresses[i]);
			}
			
			mimeMessage.setSubject(title);
			mimeMessage.setRecipients(Message.RecipientType.TO, emailAddressTo);
			Multipart multipart = new MimeMultipart();
			MimeBodyPart mailContent = new MimeBodyPart();
			mailContent.setContent(content, "text/html;charset=utf-8");
			multipart.addBodyPart(mailContent);
			mimeMessage.setContent(multipart);
			mimeMessage.setSentDate(new Date());
			Transport.send(mimeMessage);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值