• Java实现发送邮件功能


    代码:

    import java.util.Properties;
    
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeUtility;
    
    import org.apache.commons.lang3.StringUtils;
    
    
    public class EmailUtil {
    
    	/**
    	 * 发送邮件
    	 * @param to 接收邮件邮箱号
    	 * @param cc 抄送邮件邮箱号
    	 * @param subject 邮件标题
    	 * @param content 邮件内容
    	 * @return
    	 */
    	public boolean sendEmail(String to, String cc, String subject, String content) {
    		if (StringUtils.isEmpty(to)) {
    			return false;
    		}
    		EmailConfig config = createEmailConfig(to, cc);
    		try {
    			return send(config, subject, content);
    		} catch (Exception e) {
    			e.printStackTrace();
    			return false;
    		}
    	}
    	
    	private static EmailConfig createEmailConfig(String to, String cc) {
    		EmailConfig config = new EmailConfig();
    		config.setHost("发送邮件服务器");
    		config.setPort("服务器发送端口");
    		config.setEmail("系统使用邮箱号");
    		config.setUserName("系统发送邮件用户名");
    		config.setPassword("系统发送邮件所用邮箱密码");
    		config.setToEmails("接收邮件邮箱号");
    		config.setCcEmails("抄送邮件邮箱号");
    		return config;
    	}
    	
    	private static boolean send(EmailConfig config, String subject, String content) {
    		Session session = EmailUtil.creatSession(config);
    		try {
    			MimeMessage message = new MimeMessage(session);
    			// 设置邮件头及编码格式
    			subject = MimeUtility.encodeWord(subject, "UTF-8", "Q");
    			message.setSubject(subject);
    			// 邮件署名
    			message.setFrom(new InternetAddress(config.getEmail(), config.getUserName()));
    			// 消息接收者(收件地址)
    			if (StringUtils.isNotBlank(config.getToEmails())) {
    				String[] emails = config.getToEmails().split(",");
    				InternetAddress[] addresses = new InternetAddress[emails.length];
    				for (int i = 0; i < emails.length; i++) {
    					addresses[i] = new InternetAddress(emails[i]);
    				}
    				message.addRecipients(Message.RecipientType.TO, addresses);
    			}
    			// 消息抄送者(收件地址)
    			if (StringUtils.isNotBlank(config.getCcEmails())) {
    				String[] emails = config.getCcEmails().split(",");
    				InternetAddress[] addresses = new InternetAddress[emails.length];
    				for (int i = 0; i < emails.length; i++) {
    					addresses[i] = new InternetAddress(emails[i]);
    				}
    				message.addRecipients(Message.RecipientType.CC, addresses);
    			}
    			message.saveChanges();
    			// 设置邮件内容及编码格式
    			// 后一个参数可以不指定编码,如"text/plain",但是将不能显示中文字符
    			message.setContent(content, "text/plain;charset=UTF-8");
    			// 发送
    			Transport.send(message);
    		} catch (Exception e) {
    			e.printStackTrace();
    			return false;
    		}
    		return true;
    	}
    	
    	private static Session creatSession(EmailConfig config) {
    		Properties props = new Properties();
    		props.setProperty("mail.transport.protocol", "SMTP");
    		props.setProperty("mail.smtp.host", config.getHost());
    		props.setProperty("mail.smtp.port", config.getPort());
    		props.setProperty("mail.smtp.auth", "true");
    		props.setProperty("mail.smtp.ssl.enable", "true");
    		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    		props.setProperty("mail.smtp.socketFactory.fallback", "false");
    		props.setProperty("mail.smtp.socketFactory.port", config.getPort());
    		props.setProperty("mail.smtp.connectiontimeout", "5000");
    		Session session = Session.getInstance(props, new Authenticator() {
    			@Override
    			protected PasswordAuthentication getPasswordAuthentication() {
    				// 登录用户名密码
    				return new PasswordAuthentication(config.getEmail(), config.getPassword());
    			}
    		});
    		// session.setDebug(true);
    		return session;
    	}
    }
    
  • 相关阅读:
    .NET Core开发实战(第10课:环境变量配置提供程序)--学习笔记
    .NET Core开发实战(第9课:命令行配置提供程序)--学习笔记
    .NET Core开发实战(第8课:配置框架:让服务无缝适应各种环境)--学习笔记
    .NET Core开发实战(第7课:用Autofac增强容器能力)--学习笔记(下)
    .NET Core开发实战(第7课:用Autofac增强容器能力)--学习笔记(上)
    .NET Core开发实战(第6课:作用域与对象释放行为)--学习笔记(下)
    .NET Core开发实战(第6课:作用域与对象释放行为)--学习笔记(上)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(下)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(中)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(上)
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/9964312.html
Copyright © 2020-2023  润新知