Java 发送邮件
来源:互联网 发布:12306手机购票软件 编辑:程序博客网 时间:2024/06/03 14:11
Java 发送邮件
使用Java应用程序发送 E-mail 十分简单,但是首先你应该在你的机器上安装 JavaMail API 和Java Activation Framework (JAF) 。
- 您可以从 Java 网站下载最新版本的 JavaMail,打开网页右侧有个 Downloads 链接,点击它下载。
- 您可以从 Java 网站下载最新版本的 JAF(版本 1.1.1)。
你也可以使用本站提供的下载链接:
- JavaMail mail.jar 1.4.5
- JAF(版本 1.1.1) activation.jar
下载并解压缩这些文件,在新创建的顶层目录中,您会发现这两个应用程序的一些 jar 文件。您需要把 mail.jar 和activation.jar 文件添加到您的 CLASSPATH 中。
如果你使用第三方邮件服务器如QQ的SMTP服务器,可查看文章底部用户认证完整的实例。
发送一封简单的 E-mail
下面是一个发送简单E-mail的例子。假设你的localhost已经连接到网络。
SendEmail.java 文件代码:
importjava.util.*;importjavax.mail.*;importjavax.mail.internet.*;importjavax.activation.*;publicclassSendEmail{publicstaticvoidmain(String[]args){Stringto ="abcd@gmail.com";Stringfrom ="web@gmail.com";Stringhost ="localhost";Propertiesproperties =System.getProperties();properties.setProperty("mail.smtp.host",host);Sessionsession =Session.getDefaultInstance(properties);try{MimeMessagemessage =newMimeMessage(session);message.setFrom(newInternetAddress(from));message.addRecipient(Message.RecipientType.TO,newInternetAddress(to));message.setSubject("This is the Subject Line!");message.setText("This is actual message");Transport.send(message);System.out.println("Sent message successfully....");}catch(MessagingExceptionmex){mex.printStackTrace();}}}
编译并运行这个程序来发送一封简单的E-mail:
$ java SendEmailSent message successfully....
如果你想发送一封e-mail给多个收件人,那么使用下面的方法来指定多个收件人ID:
voidaddRecipients(Message.RecipientTypetype,Address[]addresses)throwsMessagingException
下面是对于参数的描述:
type:要被设置为 TO, CC 或者 BCC,这里 CC 代表抄送、BCC 代表秘密抄送。举例:Message.RecipientType.TO
addresses: 这是 email ID 的数组。在指定电子邮件 ID 时,你将需要使用 InternetAddress() 方法。
发送一封 HTML E-mail
下面是一个发送 HTML E-mail 的例子。假设你的 localhost 已经连接到网络。
和上一个例子很相似,除了我们要使用 setContent() 方法来通过第二个参数为 "text/html",来设置内容来指定要发送HTML 内容。
SendHTMLEmail.java 文件代码:
importjava.util.*;importjavax.mail.*;importjavax.mail.internet.*;importjavax.activation.*;publicclassSendHTMLEmail{publicstaticvoidmain(String[]args){Stringto ="abcd@gmail.com";Stringfrom ="web@gmail.com";Stringhost ="localhost";Propertiesproperties =System.getProperties();properties.setProperty("mail.smtp.host",host);Sessionsession =Session.getDefaultInstance(properties);try{MimeMessagemessage =newMimeMessage(session);message.setFrom(newInternetAddress(from));message.addRecipient(Message.RecipientType.TO,newInternetAddress(to));message.setSubject("This is the Subject Line!");message.setContent("<h1>This is actual message</h1>","text/html");Transport.send(message);System.out.println("Sent message successfully....");}catch(MessagingExceptionmex){mex.printStackTrace();}}}
编译并运行此程序来发送HTML e-mail:
$ java SendHTMLEmailSent message successfully....
发送带有附件的 E-mail
下面是一个发送带有附件的 E-mail的例子。假设你的localhost已经连接到网络。
SendFileEmail.java 文件代码:
importjava.util.*;importjavax.mail.*;importjavax.mail.internet.*;importjavax.activation.*;publicclassSendFileEmail{publicstaticvoidmain(String[]args){Stringto ="abcd@gmail.com";Stringfrom ="web@gmail.com";Stringhost ="localhost";Propertiesproperties =System.getProperties();properties.setProperty("mail.smtp.host",host);Sessionsession =Session.getDefaultInstance(properties);try{MimeMessagemessage =newMimeMessage(session);message.setFrom(newInternetAddress(from));message.addRecipient(Message.RecipientType.TO,newInternetAddress(to));message.setSubject("This is the Subject Line!");BodyPartmessageBodyPart =newMimeBodyPart();messageBodyPart.setText("This is message body");Multipartmultipart =newMimeMultipart();multipart.addBodyPart(messageBodyPart);messageBodyPart =newMimeBodyPart();Stringfilename ="file.txt";DataSourcesource =newFileDataSource(filename);messageBodyPart.setDataHandler(newDataHandler(source));messageBodyPart.setFileName(filename);multipart.addBodyPart(messageBodyPart);message.setContent(multipart);Transport.send(message);System.out.println("Sent message successfully....");}catch(MessagingExceptionmex){mex.printStackTrace();}}}
编译并运行你的程序来发送一封带有附件的邮件。
$ java SendFileEmailSent message successfully....
用户认证部分
如果需要提供用户名和密码给e-mail服务器来达到用户认证的目的,你可以通过如下设置来完成:
props.put("mail.smtp.auth","true");props.setProperty("mail.user","myuser");props.setProperty("mail.password","mypwd");
e-mail其他的发送机制和上述保持一致。
需要用户名密码验证邮件发送实例:
本实例以 QQ 邮件服务器为例,你需要在登录QQ邮箱后台在"设置"=》账号中开启POP3/SMTP服务 ,如下图所示:
QQ 邮箱通过生成授权码来设置密码:
Java 代码如下:
SendEmail2.java 文件代码:
importjava.util.Properties;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;publicclassSendEmail2{publicstaticvoidmain(String[]args){Stringto ="xxx@qq.com";Stringfrom ="xxx@qq.com";Stringhost ="smtp.qq.com";Propertiesproperties =System.getProperties();properties.setProperty("mail.smtp.host",host);properties.put("mail.smtp.auth","true");Sessionsession =Session.getDefaultInstance(properties,newAuthenticator(){publicPasswordAuthenticationgetPasswordAuthentication(){returnnewPasswordAuthentication("xxx@qq.com","qq邮箱密码");}});try{MimeMessagemessage =newMimeMessage(session);message.setFrom(newInternetAddress(from));message.addRecipient(Message.RecipientType.TO,newInternetAddress(to));message.setSubject("This is the Subject Line!");message.setText("This is actual message");Transport.send(message);System.out.println("Sent message successfully....from runoob.com");}catch(MessagingExceptionmex){mex.printStackTrace();}}}