Java发送Email/邮件(转载)

使用Java应用程序发送邮件是很简单的,需要安装JavaMail API 和Java Activation Framework (JAF) 在机器上。

下载并解压缩这些文件,在新创建的顶层目录,会发现一些jar文件应用。需要添加了mail.jar和activation.jar文件在CLASSPATH中。

发送一个简单的E-mail:

下面是一个例子,从机器发送一个简单的电子邮件。这里假设你的电脑已经可以连接到互联网,并有足够的能力来发送电子邮件。

 1 // File Name SendEmail.java
 2 
 3 import java.util.*;
 4 import javax.mail.*;
 5 import javax.mail.internet.*;
 6 import javax.activation.*;
 7 
 8 public class SendEmail
 9 {
10    public static void main(String [] args)
11    {    
12       // Recipient's email ID needs to be mentioned.
13       String to = "abcd@gmail.com";
14 
15       // Sender's email ID needs to be mentioned
16       String from = "web@gmail.com";
17 
18       // Assuming you are sending email from localhost
19       String host = "localhost";
20 
21       // Get system properties
22       Properties properties = System.getProperties();
23 
24       // Setup mail server
25       properties.setProperty("mail.smtp.host", host);
26 
27       // Get the default Session object.
28       Session session = Session.getDefaultInstance(properties);
29 
30       try{
31          // Create a default MimeMessage object.
32          MimeMessage message = new MimeMessage(session);
33 
34          // Set From: header field of the header.
35          message.setFrom(new InternetAddress(from));
36 
37          // Set To: header field of the header.
38          message.addRecipient(Message.RecipientType.TO,
39                                   new InternetAddress(to));
40 
41          // Set Subject: header field
42          message.setSubject("This is the Subject Line!");
43 
44          // Now set the actual message
45          message.setText("This is actual message");
46 
47          // Send message
48          Transport.send(message);
49          System.out.println("Sent message successfully....");
50       }catch (MessagingException mex) {
51          mex.printStackTrace();
52       }
53    }
54 }

如果想发送电子邮件给多个收件人,然后下面的方法将被用来指定多个电子邮件标识:

void addRecipients(Message.RecipientType type,Address[] addresses)throws MessagingException

这里的参数的说明:

  • type: 这将被设定为TO,CC或BCC。在这里,CC代表抄送和BCC代表黑抄送。例子Message.RecipientType.TO

  • addresses: 这是电子邮件的ID的数组。需要使用InternetAddress() 方法,同时指定电子邮件ID

Send an HTML E-mail:

下面是一个例子,从机器发送的HTML电子邮件。这里假设你的本地主机连接到互联网,并有足够的能力来发送电子邮件。

这个例子非常相似,前一个,除了在这里我们使用的是使用setContent()方法来设置内容的第二个参数是"text/html"类型指定的HTML内容被包含在消息中。

通过这个例子,可以发送大如喜欢HTML内容。

 1 // File Name SendHTMLEmail.java
 2 
 3 import java.util.*;
 4 import javax.mail.*;
 5 import javax.mail.internet.*;
 6 import javax.activation.*;
 7 
 8 public class SendHTMLEmail
 9 {
10    public static void main(String [] args)
11    {
12       
13       // Recipient's email ID needs to be mentioned.
14       String to = "abcd@gmail.com";
15 
16       // Sender's email ID needs to be mentioned
17       String from = "web@gmail.com";
18 
19       // Assuming you are sending email from localhost
20       String host = "localhost";
21 
22       // Get system properties
23       Properties properties = System.getProperties();
24 
25       // Setup mail server
26       properties.setProperty("mail.smtp.host", host);
27 
28       // Get the default Session object.
29       Session session = Session.getDefaultInstance(properties);
30 
31       try{
32          // Create a default MimeMessage object.
33          MimeMessage message = new MimeMessage(session);
34 
35          // Set From: header field of the header.
36          message.setFrom(new InternetAddress(from));
37 
38          // Set To: header field of the header.
39          message.addRecipient(Message.RecipientType.TO,
40                                   new InternetAddress(to));
41 
42          // Set Subject: header field
43          message.setSubject("This is the Subject Line!");
44 
45          // Send the actual HTML message, as big as you like
46          message.setContent("<h1>This is actual message</h1>",
47                             "text/html" );
48 
49          // Send message
50          Transport.send(message);
51          System.out.println("Sent message successfully....");
52       }catch (MessagingException mex) {
53          mex.printStackTrace();
54       }
55    }
56 }

发送附件的电子邮件:

下面是一个例子与附件从机器发送电子邮件。这里假设你的本地主机连接到互联网,并有足够的能力来发送电子邮件。

 1 // File Name SendFileEmail.java
 2 
 3 import java.util.*;
 4 import javax.mail.*;
 5 import javax.mail.internet.*;
 6 import javax.activation.*;
 7 
 8 public class SendFileEmail
 9 {
10    public static void main(String [] args)
11    {
12       
13       // Recipient's email ID needs to be mentioned.
14       String to = "abcd@gmail.com";
15 
16       // Sender's email ID needs to be mentioned
17       String from = "web@gmail.com";
18 
19       // Assuming you are sending email from localhost
20       String host = "localhost";
21 
22       // Get system properties
23       Properties properties = System.getProperties();
24 
25       // Setup mail server
26       properties.setProperty("mail.smtp.host", host);
27 
28       // Get the default Session object.
29       Session session = Session.getDefaultInstance(properties);
30 
31       try{
32          // Create a default MimeMessage object.
33          MimeMessage message = new MimeMessage(session);
34 
35          // Set From: header field of the header.
36          message.setFrom(new InternetAddress(from));
37 
38          // Set To: header field of the header.
39          message.addRecipient(Message.RecipientType.TO,
40                                   new InternetAddress(to));
41 
42          // Set Subject: header field
43          message.setSubject("This is the Subject Line!");
44 
45          // Create the message part 
46          BodyPart messageBodyPart = new MimeBodyPart();
47 
48          // Fill the message
49          messageBodyPart.setText("This is message body");
50          
51          // Create a multipar message
52          Multipart multipart = new MimeMultipart();
53 
54          // Set text message part
55          multipart.addBodyPart(messageBodyPart);
56 
57          // Part two is attachment
58          messageBodyPart = new MimeBodyPart();
59          String filename = "file.txt";
60          DataSource source = new FileDataSource(filename);
61          messageBodyPart.setDataHandler(new DataHandler(source));
62          messageBodyPart.setFileName(filename);
63          multipart.addBodyPart(messageBodyPart);
64 
65          // Send the complete message parts
66          message.setContent(multipart );
67 
68          // Send message
69          Transport.send(message);
70          System.out.println("Sent message successfully....");
71       }catch (MessagingException mex) {
72          mex.printStackTrace();
73       }
74    }
75 }

用户认证部分:

如果需要提供用户ID和密码的电子邮件服务器进行身份验证的目的,那么可以按如下方法设置这些属性:

1  props.setProperty("mail.user", "myuser");
2  props.setProperty("mail.password", "mypwd");

邮件发送机制的其余部分将继续如上面所述。

本文转载自:http://www.yiibai.com/java/java_sending_email.html

posted @ 2017-02-08 17:08  Johnson_wang  阅读(191)  评论(0)    收藏  举报