赞
踩
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.3</version>
- </dependency>
- @Test()
- public void testSend() throws Exception {
- // 连接邮件服务器的参数配置
- Properties props = new Properties();
- // 设置用户的认证方式
- props.setProperty("mail.smtp.auth", "true");
- props.put("mail.smtp.host", host);
- props.put("mail.smtp.port", port);
- // 创建定义整个应用程序所需的环境信息的 Session 对象
- Session session = Session.getInstance(props,new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(name, pwd);
- }
- });
- // 创建消息对象
- MimeMessage message = new MimeMessage(session);
- // 邮件消息头
- message.setFrom(new InternetAddress(name)); // 发件人
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 收件人
- message.setSubject("邮件测试1");
- message.setText("Hello, java mail!");
- // 发送邮件
- Transport.send(message);
- }
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>4.3.14.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.3</version>
- </dependency>
- @Test
- public void testSend2() throws MessagingException {
- JavaMailSenderImpl sendService = new JavaMailSenderImpl();
- sendService.setHost(host);
- sendService.setPort(port);
- sendService.setUsername(name);
- sendService.setPassword(pwd);
- MimeMessage msg = sendService.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
- helper.setTo(to);
- helper.setFrom(name);
- helper.setSubject("邮件测试2");
- helper.setText("Hello, spring!", true);
- sendService.send(msg);
- }
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-email</artifactId>
- <version>1.4</version>
- </dependency>
-
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.3</version>
- </dependency>
- @Test
- public void testSend3() throws EmailException {
- Email email = new SimpleEmail();
- email.setHostName(host);
- email.setSmtpPort(port);
- email.setAuthenticator(new DefaultAuthenticator(name, pwd));
- email.setCharset("UTF-8");
- email.setFrom(name);
- email.addTo(to);
- email.setSubject("测试邮件3");
- email.setMsg("Hello, commons-email!");
- email.send();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。