Java教程 专题
专题目录
您的位置:java > Java教程专题 > Java 发送邮件
Java 发送邮件
作者:--    发布时间:2019-11-20

使用java应用程序发送e-mail十分简单,但是首先你应该在你的机器上安装javamail api 和java activation framework (jaf) 。

你可以在 javamail (version 1.2) 下载最新的版本。

你可以再 在jaf (version 1.1.1)下载最新的版本。

下载并解压这些文件,最上层文件夹你会发现很多的jar文件。你需要将mail.jar和activation.jar 添加到你的classpath中。

如果你使用第三方邮件服务器如qq的smtp服务器,可查看文章底部用户认证完整的实例。


发送一封简单的 e-mail

下面是一个发送简单e-mail的例子。假设你的localhost已经连接到网络。

// 文件名 sendemail.java
 
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
 
public class sendemail
{
   public static void main(string [] args)
   {   
      // 收件人电子邮箱
      string to = "abcd@gmail.com";
 
      // 发件人电子邮箱
      string from = "web@gmail.com";
 
      // 指定发送邮件的主机为 localhost
      string host = "localhost";
 
      // 获取系统属性
      properties properties = system.getproperties();
 
      // 设置邮件服务器
      properties.setproperty("mail.smtp.host", host);
 
      // 获取默认session对象
      session session = session.getdefaultinstance(properties);
 
      try{
         // 创建默认的 mimemessage 对象
         mimemessage message = new mimemessage(session);
 
         // set from: 头部头字段
         message.setfrom(new internetaddress(from));
 
         // set to: 头部头字段
         message.addrecipient(message.recipienttype.to,
                                  new internetaddress(to));
 
         // set subject: 头部头字段
         message.setsubject("this is the subject line!");
 
         // 设置消息体
         message.settext("this is actual message");
 
         // 发送消息
         transport.send(message);
         system.out.println("sent message successfully....");
      }catch (messagingexception mex) {
         mex.printstacktrace();
      }
   }
}

编译并运行这个程序来发送一封简单的e-mail:

$ java sendemail
sent message successfully....

如果你想发送一封e-mail给多个收件人,那么使用下面的方法来指定多个收件人id:

void addrecipients(message.recipienttype type,
                   address[] addresses)
throws messagingexception

下面是对于参数的描述:

  • type:要被设置为to, cc 或者bcc. 这里cc 代表抄送、bcc 代表秘密抄送y. 举例:message.recipienttype.to

  • addresses: 这是email id的数组。在指定电子邮件id时,你将需要使用internetaddress()方法。


发送一封html e-mail

下面是一个发送html e-mail的例子。假设你的localhost已经连接到网络。

和上一个例子很相似,除了我们要使用setcontent()方法来通过第二个参数为"text/html",来设置内容来指定要发送html内容。

// 文件名 sendhtmlemail.java
 
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
 
public class sendhtmlemail
{
   public static void main(string [] args)
   {
     
      // 收件人电子邮箱
      string to = "abcd@gmail.com";
 
      // 发件人电子邮箱
      string from = "web@gmail.com";
 
      // 指定发送邮件的主机为 localhost
      string host = "localhost";
 
      // 获取系统属性
      properties properties = system.getproperties();
 
      // 设置邮件服务器
      properties.setproperty("mail.smtp.host", host);
 
      // 获取默认的 session 对象。
      session session = session.getdefaultinstance(properties);
 
      try{
         // 创建默认的 mimemessage 对象。
         mimemessage message = new mimemessage(session);
 
         // set from: 头部头字段
         message.setfrom(new internetaddress(from));
 
         // set to: 头部头字段
         message.addrecipient(message.recipienttype.to,
                                  new internetaddress(to));
 
         // set subject: 头字段
         message.setsubject("this is the subject line!");
 
         // 发送 html 消息, 可以插入html标签
         message.setcontent("<h1>this is actual message</h1>",
                            "text/html" );
 
         // 发送消息
         transport.send(message);
         system.out.println("sent message successfully....");
      }catch (messagingexception mex) {
         mex.printstacktrace();
      }
   }
}

编译并运行此程序来发送html e-mail:

$ java sendhtmlemail
sent message successfully....

发送带有附件的 e-mail

下面是一个发送带有附件的 e-mail的例子。假设你的localhost已经连接到网络。

// 文件名 sendfileemail.java
 
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
 
public class sendfileemail
{
   public static void main(string [] args)
   {
     
      // 收件人电子邮箱
      string to = "abcd@gmail.com";
 
      // 发件人电子邮箱
      string from = "web@gmail.com";
 
      // 指定发送邮件的主机为 localhost
      string host = "localhost";
 
      // 获取系统属性
      properties properties = system.getproperties();
 
      // 设置邮件服务器
      properties.setproperty("mail.smtp.host", host);
 
      // 获取默认的 session 对象。
      session session = session.getdefaultinstance(properties);
 
      try{
         // 创建默认的 mimemessage 对象。
         mimemessage message = new mimemessage(session);
 
         // set from: 头部头字段
         message.setfrom(new internetaddress(from));
 
         // set to: 头部头字段
         message.addrecipient(message.recipienttype.to,
                                  new internetaddress(to));
 
         // set subject: 头字段
         message.setsubject("this is the subject line!");
 
         // 创建消息部分
         bodypart messagebodypart = new mimebodypart();
 
         // 消息
         messagebodypart.settext("this is message body");
        
         // 创建多重消息
         multipart multipart = new mimemultipart();
 
         // 设置文本消息部分
         multipart.addbodypart(messagebodypart);
 
         // 附件部分
         messagebodypart = new mimebodypart();
         string filename = "file.txt";
         datasource source = new filedatasource(filename);
         messagebodypart.setdatahandler(new datahandler(source));
         messagebodypart.setfilename(filename);
         multipart.addbodypart(messagebodypart);
 
         // 发送完整消息
         message.setcontent(multipart );
 
         //   发送消息
         transport.send(message);
         system.out.println("sent message successfully....");
      }catch (messagingexception mex) {
         mex.printstacktrace();
      }
   }
}

编译并运行你的程序来发送一封带有附件的邮件。

$ java sendfileemail
sent 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服务 ,如下图所示:

qqmailset

java 代码如下:

// 需要用户名密码邮件发送实例
//文件名 sendemail2.java
//本实例以qq邮箱为例,你需要在qq后台设置

import java.util.properties;

import javax.mail.authenticator;
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 sendemail2
{
   public static void main(string [] args)
   {
      // 收件人电子邮箱
      string to = "xxx@qq.com";

      // 发件人电子邮箱
      string from = "xxx@qq.com";

      // 指定发送邮件的主机为 localhost
      string host = "smtp.qq.com";  //qq 邮件服务器

      // 获取系统属性
      properties properties = system.getproperties();

      // 设置邮件服务器
      properties.setproperty("mail.smtp.host", host);

      properties.put("mail.smtp.auth", "true");
      // 获取默认session对象
      session session = session.getdefaultinstance(properties,new authenticator(){
	    public passwordauthentication getpasswordauthentication()
	    {
	     return new passwordauthentication("xxx@qq.com", "qq邮箱密码"); //发件人邮件用户名、密码
	    }
	   });

      try{
         // 创建默认的 mimemessage 对象
         mimemessage message = new mimemessage(session);

         // set from: 头部头字段
         message.setfrom(new internetaddress(from));

         // set to: 头部头字段
         message.addrecipient(message.recipienttype.to,
                                  new internetaddress(to));

         // set subject: 头部头字段
         message.setsubject("this is the subject line!");

         // 设置消息体
         message.settext("this is actual message");

         // 发送消息
         transport.send(message);
         system.out.println("sent message successfully....from w3cschool.cn");
      }catch (messagingexception mex) {
         mex.printstacktrace();
      }
   }
}
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册