使用JavaMail SMTP协议发送邮件

来源:互联网 发布:电脑c语言编译器 编辑:程序博客网 时间:2024/05/03 18:00

使用JavaMail SMTP协议发送邮件

最近需要实现通过发送邮件让用户找回密码的功能,自己用Socket写了SMTP协议的邮件发送程序,但是很多邮件服务器的anti-spam需要验证发送邮箱的合法性,所以只得放弃,后来发现用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
<span style="font-size:16px;">importjava.util.Properties;
  
importjavax.mail.Message;
importjavax.mail.MessagingException;
importjavax.mail.PasswordAuthentication;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
  
publicclass SendMailTLS {
  
    publicstatic void main(String[] args) {
  
        finalString username = "someoe@gmail.com";
        finalString password = "password";
  
        Properties props = newProperties();
        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,
          newjavax.mail.Authenticator() {
            protectedPasswordAuthentication getPasswordAuthentication() {
                returnnew PasswordAuthentication(username, password);
            }
          });
  
        try{
  
            Message message = newMimeMessage(session);
            message.setFrom(newInternetAddress("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) {
            thrownew 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;">importjava.util.Properties;
importjavax.mail.Message;
importjavax.mail.MessagingExcept</span>ion;
importjavax.mail.PasswordAuthentication;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
  
publicclass SendMailSSL {
    publicstatic void main(String[] args) {
        Properties props = newProperties();
        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,
            newjavax.mail.Authenticator() {
                protectedPasswordAuthentication getPasswordAuthentication() {
                    returnnew PasswordAuthentication("username","password");
                }
            });
  
        try{
  
            Message message = newMimeMessage(session);
            message.setFrom(newInternetAddress("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) {
            thrownew RuntimeException(e);
        }
    }
}</span>

0 0
原创粉丝点击