Android开发中,当忘记密码需要找回密码的时候,很多情况都是向注册邮箱自动发送注册登录密码,自己写的一个自动发送邮件的java类,亲测可用。
直接贴代码:
public class SendMail { static int port = 25;//端口号 static String server = "smtp.163.com";// 邮件服务器mail.cpip.net.cn static String from = "小明";// 发送者,显示的发件人名字 static String user = "[email protected]";// 发送者邮箱地址 static String password = "xxxxxx";// 密码 public static void sendEmail(String email, String subject, String body) throws UnsupportedEncodingException { try { Properties props = new Properties(); props.put("mail.smtp.host", server); props.put("mail.smtp.port", String.valueOf(port)); props.put("mail.smtp.auth", "true"); Transport transport = null; Session session = Session.getDefaultInstance(props, null); transport = session.getTransport("smtp"); transport.connect(server, user, password); MimeMessage msg = new MimeMessage(session); msg.setSentDate(new Date()); InternetAddress fromAddress = new InternetAddress(user, from, "UTF-8"); msg.setFrom(fromAddress); InternetAddress[] toAddress = new InternetAddress[1]; toAddress[0] = new InternetAddress(email); msg.setRecipients(Message.RecipientType.TO, toAddress); msg.setSubject(subject, "UTF-8"); msg.setText(body, "UTF-8"); msg.saveChanges(); transport.sendMessage(msg, msg.getAllRecipients()); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10626.html