Java邮箱自动发送邮件
发布时间:2020-12-14 23:17:52 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class SendMail { static int port = 25;//端口号 static String server = "smtp.163.com";// 邮件服务器mail.cpip.net.cn static String from
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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(); } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |