天天看點

java郵件發送的簡單實作,使用javamail通過smtp協定發信

1.通過javamail實作      
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SimpleAliDMSendMail {
    private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
    private static final int ALIDM_SMTP_PORT = 25;

    public static void main(String[] args) throws MessagingException {
        // 配置發送郵件的環境屬性
        final Properties props = new Properties();
        // 表示SMTP發送郵件,需要進行身份驗證
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", ALIDM_SMTP_HOST);
        props.put("mail.smtp.port", ALIDM_SMTP_PORT);   
        // 如果使用ssl,則去掉使用25端口的配置,進行如下配置, 
        // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        // props.put("mail.smtp.socketFactory.port", "465");
        // props.put("mail.smtp.port", "465");


        // 發件人的賬号
        props.put("mail.user", "***");
        // 通路SMTP服務時需要提供的密碼
        props.put("mail.password", "***");

        // 建構授權資訊,用于進行SMTP進行身份驗證
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 使用者名、密碼
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用環境屬性和授權資訊,建立郵件會話
        Session mailSession = Session.getInstance(props, authenticator);
        // 建立郵件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 設定發件人
        InternetAddress form = new InternetAddress(
                props.getProperty("mail.user"));
        message.setFrom(form);

        // 設定收件人
        InternetAddress to = new InternetAddress("***");
        message.setRecipient(MimeMessage.RecipientType.TO, to);

        // 設定郵件标題
        message.setSubject("測試郵件");
        // 設定郵件的内容體
        message.setContent("測試的HTML郵件", "text/html;charset=UTF-8");

        // 發送郵件
        Transport.send(message);
    }
}
           
2.使用Apache Commons-email元件發送郵件      
commons-email是apache提供的一個開源的API,是對javamail的封裝,
    
    
     是以在使用時要将javamail.jar加到 class path中,
    
    
     主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment四個類。
     
 

     SimpleEmail:發送簡單的email,不能添加附件
MultiPartEmail:文本郵件,可以添加多個附件
HtmlEmail:HTML格式郵件,同時具有MultiPartEmail類所有“功能”
EmailAttchment:附件類,可以添加本地資源,也可以指定網絡上資源,在發送時自動将網絡上資源下載下傳發送。
     
 
發送基本文本格式郵件:

            
SimpleEmail email = new SimpleEmail();
//smtp host 
email.setHostName("mail.test.com");
//登陸郵件伺服器的使用者名和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("[email protected]", "John Doe");
//發送人
email.setFrom("[email protected]", "Me");
//标題
email.setSubject("Test message");
//郵件内容
email.setMsg("This is a simple test of commons-email");
//發送
email.send();
           
發送文本格式,帶附件郵件:
//附件,可以定義多個附件對象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e:\\1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
//smtp host 
email.setHostName("mail.test.com");
//登陸郵件伺服器的使用者名和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("[email protected]", "John Doe");
//發送人
email.setFrom("[email protected]", "Me");
//标題
email.setSubject("Test message");
//郵件内容
email.setMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//發送
email.send();
           
  發送HTML格式帶附件郵件:
//附件,可以定義多個附件對象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e:\\1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
HtmlEmail email = new HtmlEmail ();
//smtp host 
email.setHostName("mail.test.com");
//登陸郵件伺服器的使用者名和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("[email protected]", "John Doe");
//發送人
email.setFrom("[email protected]", "Me");
//标題
email.setSubject("Test message");
//郵件内容
email.setHtmlMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//發送
email.send();
           
下面提供一個完整的程式示例:
package test

import org.apache.commons.mail.*;

public class SendEMail
{
    public static void main ( String[] arg ) throws Exception
    {
        SimpleEmail email = new SimpleEmail ( );

        // smtp host
        email.setHostName ( "smtp.163.com" );
        // 登陸郵件伺服器的使用者名和密碼
        email.setAuthentication ( "test", "123456" );
        // 接收人
        email.addTo ( "[email protected]", "test" );
        // 發送人
        email.setFrom ( "[email protected]", "測試者" );
        // 标題
        email.setSubject ( "Test message" );
        // 郵件内容
        email.setMsg ( "This is a simple test of commons-email 測試......" );
        // 發送
        email.send ( );
        
        System.out.println ( "發送成功 Send email successful!" );

    }
}