标签:
/* @param fromMail 发送邮箱 * @param user 发送邮箱账号 * @param password 邮箱密码 * @param toMail 接收邮件地址 * @param mailTitle 邮件标题 * @param mailContent 邮件内容 * @throws Exception */ public static void sendMail(String fromMail, String user, String password, String toMail, String mailTitle, String mailContent) throws Exception { MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); Properties props = new Properties(); props.put("mail.smtp.host", "mail.*****.com"); props.put("mail.smtp.port", 465); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); Session session = Session.getInstance(props); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(fromMail)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail)); message.setSubject(mailTitle); message.setText(mailContent); message.setSentDate(new Date()); message.saveChanges(); Transport transport = session.getTransport(); transport.connect(user, password); transport.sendMessage(message, message.getAllRecipients()); transport.close(); }
下面是需要添加的包:
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
标签:
原文地址:http://my.oschina.net/u/2370543/blog/520025