java mail 邮件发送实例【搬】

说明:转自国外博客:欢迎查阅原作

该实例较新,简明易懂,值得新手借鉴

以gmail为例,注意:

1.通过TLS方式发送

 1 package com.mkyong.common;
 2
 3 import java.util.Properties;
 4
 5 import javax.mail.Message;
 6 import javax.mail.MessagingException;
 7 import javax.mail.PasswordAuthentication;
 8 import javax.mail.Session;
 9 import javax.mail.Transport;
10 import javax.mail.internet.InternetAddress;
11 import javax.mail.internet.MimeMessage;
12
13 public class SendMailTLS {
14
15     public static void main(String[] args) {
16
17         final String username = "[email protected]";
18         final String password = "password";
19
20         Properties props = new Properties();
21         props.put("mail.smtp.auth", "true");
22         props.put("mail.smtp.starttls.enable", "true");
23         props.put("mail.smtp.host", "smtp.gmail.com");
24         props.put("mail.smtp.port", "587");
25
26         Session session = Session.getInstance(props,
27           new javax.mail.Authenticator() {
28             protected PasswordAuthentication getPasswordAuthentication() {
29                 return new PasswordAuthentication(username, password);
30             }
31           });
32
33         try {
34
35             Message message = new MimeMessage(session);
36             message.setFrom(new InternetAddress("[email protected]"));
37             message.setRecipients(Message.RecipientType.TO,
38                 InternetAddress.parse("[email protected]"));
39             message.setSubject("Testing Subject");
40             message.setText("Dear Mail Crawler,"
41                 + "\n\n No spam to my email, please!");
42
43             Transport.send(message);
44
45             System.out.println("Done");
46
47         } catch (MessagingException e) {
48             throw new RuntimeException(e);
49         }
50     }
51 }

2.通过SSL方式发送

 1 package com.mkyong.common;
 2
 3 import java.util.Properties;
 4 import javax.mail.Message;
 5 import javax.mail.MessagingException;
 6 import javax.mail.PasswordAuthentication;
 7 import javax.mail.Session;
 8 import javax.mail.Transport;
 9 import javax.mail.internet.InternetAddress;
10 import javax.mail.internet.MimeMessage;
11
12 public class SendMailSSL {
13     public static void main(String[] args) {
14         Properties props = new Properties();
15         props.put("mail.smtp.host", "smtp.gmail.com");
16         props.put("mail.smtp.socketFactory.port", "465");
17         props.put("mail.smtp.socketFactory.class",
18                 "javax.net.ssl.SSLSocketFactory");
19         props.put("mail.smtp.auth", "true");
20         props.put("mail.smtp.port", "465");
21
22         Session session = Session.getDefaultInstance(props,
23             new javax.mail.Authenticator() {
24                 protected PasswordAuthentication getPasswordAuthentication() {
25                     return new PasswordAuthentication("username","password");
26                 }
27             });
28
29         try {
30
31             Message message = new MimeMessage(session);
32             message.setFrom(new InternetAddress("[email protected]"));
33             message.setRecipients(Message.RecipientType.TO,
34                     InternetAddress.parse("[email protected]"));
35             message.setSubject("Testing Subject");
36             message.setText("Dear Mail Crawler," +
37                     "\n\n No spam to my email, please!");
38
39             Transport.send(message);
40
41             System.out.println("Done");
42
43         } catch (MessagingException e) {
44             throw new RuntimeException(e);
45         }
46     }
47 }

java mail 邮件发送实例【搬】,布布扣,bubuko.com

时间: 2024-10-12 16:59:22

java mail 邮件发送实例【搬】的相关文章

java mail邮件发送(带附件) 支持SSL

java mail邮件发送(带附件)有三个类 MailSenderInfo.java package mail; import java.util.Properties; import java.util.Vector; public class MailSenderInfo { // 发送邮件的server的IP和端口 private String mailServerHost; private String mailServerPort = "25"; // 邮件发送者的地址 pr

java mail邮件发送(带附件)

java mail邮件发送(带附件)有三个类 MailSenderInfo.java package mail; import java.util.Properties; import java.util.Vector; public class MailSenderInfo { // 发送邮件的服务器的IP和端口 private String mailServerHost; private String mailServerPort = "25"; // 邮件发送者的地址 priva

Java Mail邮件发送的简单实现

1.什么是java mail JAVA MAIL是利用现有的邮件账户发送邮件的工具,通过JAVA Mail的操控,让程序自动的使用设置的邮箱发送邮件. 这一机制被广泛的用在注册激活和垃圾邮件的发送等方面. 2.邮件发送的大致过程 1.构建一个继承自javax.mail.Authenticator的具体类,并重写里面的getPasswordAuthentication()方法.此类是用作登录校验的,以确保你对该邮箱有发送邮件的权利. 2.构建一个properties文件,该文件中存放SMTP服务器

Linux mail 邮件发送

Linux mail 邮件介绍 在Linux系统下我们可以通过"mail"命令,发送邮件,在运维中通常我们它来实现邮件告警. 安装 yum install -y sendmail.i686 yum install -ymailx.i686 启动:service sendmail start netstat -lnp | grep :25 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1102/master tcp 0 0 ::1:25 :::* LISTE

Java网络编程:利用Java mail包发送电子邮件

下面代码是利用Java mail包封装了一个发送邮件的类 import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Me

java实现邮件发送验证码

建议不要用qq邮箱,我使用qq邮箱直接一直给我报530错误,我一直认为我代码写的有错误或者POP3/SMTP服务没弄好.所以建议注册个别的邮箱,我就申请了个网易163邮箱瞬间就好了.所以去申请个别的邮箱吧. 用邮件发送验证码,首先需要jar包.我写的代码只需要mail.jar,没有的话可以给我留言,我给你发个. 废话不说了直接上代码(这个代码就是个工具类直接用就好) package com.itheima.utils; import java.util.Properties; import ja

纯java实现邮件发送服务(亲测好用)

今天自己测试了一下用java代码实现发送有限的服务,非常简单.直接贴代码: import com.sun.mail.util.MailSSLSocketFactory; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.security.GeneralSecurityException; import java.u

工具类学习-java实现邮件发送激活码

问题:用java实现服务器发送激活码到用户邮件. 步骤一:如果是个人的话,确保在本地安装邮件服务器(易邮服务器)和邮件客户端(foxmail). 步骤二:导入jar包  mail.jar,其他的需要什么协议导什么jar. package cn.itcast.store.utils; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.M

Java实现邮件发送功能

import java.util.Properties; import javax.mail.Address; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Sen