使用Java应用程序发送邮件是很简单的,需要安装JavaMail API 和Java Activation Framework (JAF) 在机器上。
可以从Java的标准网站上下载JavaMail(版本1.2)的最新版本。
可以从Java的标准网站下载最新版本的JAF(版本1.1.1)。
下载并解压缩这些文件,在新创建的顶层目录,会发现一些jar文件应用。需要添加了mail.jar和activation.jar文件在CLASSPATH中。
下面是一个例子,从机器发送一个简单的电子邮件。这里假设你的电脑已经可以连接到互联网,并有足够的能力来发送电子邮件。
// File Name SendEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail { public static void main(String [] args) { // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "web@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Now set the actual message message.setText("This is actual message"); // Send message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }
编译并运行这个程序发送一个简单的电子邮件:
$ java SendEmail Sent message successfully....
如果想发送电子邮件给多个收件人,然后下面的方法将被用来指定多个电子邮件标识:
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
这里的参数的说明:
type: 这将被设定为TO,CC或BCC。在这里,CC代表抄送和BCC代表黑抄送。例子Message.RecipientType.TO
addresses: 这是电子邮件的ID的数组。需要使用InternetAddress() 方法,同时指定电子邮件ID
下面是一个例子,从机器发送的HTML电子邮件。这里假设你的本地主机连接到互联网,并有足够的能力来发送电子邮件。
这个例子非常相似,前一个,除了在这里我们使用的是使用setContent()方法来设置内容的第二个参数是"text/html"类型指定的HTML内容被包含在消息中。
通过这个例子,可以发送大如喜欢HTML内容。
// File Name SendHTMLEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendHTMLEmail { public static void main(String [] args) { // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "web@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Send the actual HTML message, as big as you like message.setContent("<h1>This is actual message</h1>", "text/html" ); // Send message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }
编译并运行这个程序来发送HTML电子邮件:
$ java SendHTMLEmail Sent message successfully....
下面是一个例子与附件从机器发送电子邮件。这里假设你的本地主机连接到互联网,并有足够的能力来发送电子邮件。
// File Name SendFileEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendFileEmail { public static void main(String [] args) { // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "web@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart ); // Send message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }
编译并运行这个程序来发送HTML电子邮件:
$ java SendFileEmail Sent message successfully....
如果需要提供用户ID和密码的电子邮件服务器进行身份验证的目的,那么可以按如下方法设置这些属性:
props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd");
邮件发送机制的其余部分将继续如上面所述。