使用JavaMail SMTP协议发送邮件

使用JavaMail SMTP协议发送邮件

发现用javamail包可以很方便的实现。示例程序使用gmail的邮件服务器来发送邮件。关于SMTP端口等配置见下面链接:

https://support.google.com/mail/bin/answer.py?hl=en&answer=13287

注:下面程序需导入javaee-api-6.0.jar 跟 mail.jar

1.使用TLS发送邮件

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

2.使用SSL发送邮件

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

转自:http://jia1546.is-programmer.com/posts/32912.html
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值