maven 引用如下
<dependency> <groupId> org.apache.geronimo.specs</groupId> <artifactId>geronimo-javamail_1.4_spec </artifactId> <version>1.3</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.2</version> </dependency>
public void sendMail(){ try{ Properties props=new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); Session mailSession = Session.getInstance(props); //设置session,和邮件服务器进行通讯。 mailSession.setDebug(true); MimeMessage message = new MimeMessage(mailSession); message.setSubject(mail_subject); // 设置邮件主题 message.setText(mail_body); // 设置邮件正文 message.setHeader(mail_head_name, mail_head_value); // 设置邮件标题 message.setSentDate(new Date()); // 设置邮件发送日期 InternetAddress address = new InternetAddress(mail_from, personalName); message.setFrom(address); // 设置邮件发送者的地址 InternetAddress toAddress = new InternetAddress(mail_to); // 设置邮件接收方的地址 message.addRecipient(Message.RecipientType.TO, toAddress); Transport transport = null; transport = mailSession.getTransport("smtp"); message.saveChanges(); transport.connect(host, username, password); transport.sendMessage(message, message.getAllRecipients()); transport.close(); System.out.println("send success!"); }catch (Exception ex){ ex.printStackTrace(); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10843.html