首页 > 代码库 > mail.jar 发送邮件
mail.jar 发送邮件
1.spring参数注入+util 发送邮件
2.util配置参数+util发送邮件
1.spring参数注入+util 发送邮件
<bean id="mailSender" class="com.midea.ftms.util.MailSender"> <property name="host" value=http://www.mamicode.com/"${mail.smtp.host}"></property> <property name="auth" value=http://www.mamicode.com/"${mail.smtp.auth}"></property> <property name="user" value=http://www.mamicode.com/"${mail.user}"></property> <property name="password" value=http://www.mamicode.com/"${mail.passwd}"></property> <property name="from" value=http://www.mamicode.com/"${mail.from}"></property> <property name="remindNum" value=http://www.mamicode.com/"${mail.remindnum}"></property> <property name="debugModel" value=http://www.mamicode.com/"${mail.debug}"></property> </bean>
import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MailSender { private String host; private String auth; private String user; private String password; private String from; private Integer remindNum; private Boolean debugModel; public MailSender() { } /** * @see 发送邮件基础方法,请遵循使用规则 MailUtil.sendMail * @param to 邮件接收地址 * @param subject 邮件主题 * @param content 邮件内容 * @throws Exception 调用者处理异常 */ public void send(String[] to, String subject, String content) throws Exception { Properties props = new Properties(); // 指定SMTP服务器 props.put("mail.smtp.host", host); // 指定是否需要SMTP验证 props.put("mail.smtp.auth", auth); Session mailSession = Session.getDefaultInstance(props); // 是否在控制台显示debug信息 mailSession.setDebug(debugModel); Message message = new MimeMessage(mailSession); // 发件人 message.setFrom(new InternetAddress(from)); // 收件人 InternetAddress[] addresses = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { addresses[i] = new InternetAddress(to[i]); } message.setRecipients(Message.RecipientType.TO, addresses); // 邮件主题 message.setSubject("subject:"+subject); // 邮件内容(HTML格式) message.setContent(content, "text/html;charset=GBK"); // 保存设置,让设置生效 message.saveChanges(); // 发送 Transport transport = mailSession.getTransport("smtp"); transport.connect(host, user, password); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getAuth() { return auth; } public void setAuth(String auth) { this.auth = auth; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public Integer getRemindNum() { return remindNum; } public void setRemindNum(Integer remindNum) { this.remindNum = remindNum; } public Boolean getDebugModel() { return debugModel; } public void setDebugModel(Boolean debugModel) { this.debugModel = debugModel; } }
public class MailUtil { private static volatile MailSender mailSender; private MailUtil() { } public static MailSender init() { if (mailSender == null) { synchronized (MailSender.class) { if (mailSender == null) { // mailSender = new MailSender(); mailSender = (MailSender)ContextUtil.getContext().getBean("mailSender"); } } } return mailSender; } public static void sendMail(String[] to, String subject, String content) throws Exception { MailUtil.init().send(to, subject, content); } public static void main(String[] args) { MailUtil.init().setAuth("true"); MailUtil.init().setDebugModel(true); MailUtil.init().setFrom("a@b.com"); MailUtil.init().setHost("cd.com.cn"); MailUtil.init().setUser("user"); MailUtil.init().setPassword("passwd"); MailUtil.init().setRemindNum(5); try { MailUtil.sendMail(new String[]{"asdfa@qq.com","134324323@qq.com"}, "测试", "hello yoyo"); } catch (Exception e) { e.printStackTrace(); } } }
2.util配置参数+util发送邮件
mail.jar 发送邮件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。