java email 邮件的发送


import com.sun.mail.util.MailSSLSocketFactory;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 *
 * @author guomingming <guomingming@eastarnet.com>
 */


public class EmailUtil
{
    /**
     *
     * @param to 收件人电子邮箱
     * @param from 发件人电子邮箱
     * @param fromPassport 发件人的密码
     * @param host  邮件服务器
     * @param content 设置HTML内容
     * @param headContentFrom  Set From: 头部头字段
     * @param headContentTo Set To: 头部头字段
     * @param subject Set Subject: 主题文字
     * @param filename 设置要发送附件的文件路径
     * @return
     * @throws java.io.UnsupportedEncodingException
     * @throws java.security.GeneralSecurityException
     */
    public String send( String to , String from ,String fromPassport, String host,  String content, String headContentFrom, String headContentTo, String subject, String filename ) throws UnsupportedEncodingException, GeneralSecurityException, Exception
  {
    try
        {
            // 获取系统属性
            Properties properties = System.getProperties();
            
            // 设置邮件服务器
            properties.setProperty( "mail.smtp.host", host );
            
            properties.put( "mail.smtp.auth", "true" );
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts( true );
            properties.put( "mail.smtp.ssl.enable", "true" );
            properties.put( "mail.smtp.ssl.socketFactory", sf );
            // 获取默认session对象
            Session session = Session.getDefaultInstance( properties, new javax.mail.Authenticator()
            {
                @Override
                public PasswordAuthentication getPasswordAuthentication()
                {  //qq邮箱服务器账户、第三方登录授权码
                    return new PasswordAuthentication( from, fromPassport ); //发件人邮件用户名、密码
                }
            } );
            try
            {
                // 创建默认的 MimeMessage 对象
                MimeMessage message = new MimeMessage( session );
                // Set From: 头部头字段
                message.setFrom( new InternetAddress( from, headContentFrom, "UTF-8" ) );
                // Set To: 头部头字段
                message.setRecipient( MimeMessage.RecipientType.TO, new InternetAddress( to, headContentTo, "UTF-8" ) );
                // Set Subject: 主题文字
                message.setSubject( subject, "UTF-8" );
                // 创建消息部分
                BodyPart messageBodyPart = new MimeBodyPart();
                // 消息
                // messageBodyPart.setText( "这是正文" );
                // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
                Multipart mainPart = new MimeMultipart();
                // 创建一个包含HTML内容的MimeBodyPart
                BodyPart html = new MimeBodyPart();
                // 设置HTML内容
                html.setContent( "<div style='color:red;'>"+ content +"</div>", "text/html; charset=utf-8" );
                mainPart.addBodyPart( html );
                messageBodyPart.setContent( mainPart );

                // 创建多重消息
                Multipart multipart = new MimeMultipart();

                // 设置文本消息部分
                multipart.addBodyPart( messageBodyPart );
                messageBodyPart = new MimeBodyPart();
                
                //设置要发送附件的文件路径
                if( filename.length () > 0 )
                {
                    String [] filenameArray = filename.split( ",");
                    for( int i = 0;  i < filenameArray.length; i++  )
                    {
                        DataSource source = new FileDataSource( filenameArray[0] );
                        messageBodyPart.setDataHandler( new DataHandler( source ) );
                        //处理附件名称中文(附带文件路径)乱码问题
                        messageBodyPart.setFileName( MimeUtility.encodeText( filenameArray[0] ) );
                        multipart.addBodyPart( messageBodyPart );
                    }
                }
                // 发送完整消息
                message.setContent( multipart );
                // 发送消息
                Transport.send( message );
            }
            catch( MessagingException mex )
            {
                mex.printStackTrace();
            }
            catch ( UnsupportedEncodingException ex )
            {
                throw new UnsupportedEncodingException( ex.getMessage () );
            }
            catch ( Exception ex )
            {
                throw new Exception( ex.getMessage () );
            }
        }
    catch( GeneralSecurityException ex )
    {
            throw new GeneralSecurityException( ex.getMessage () );
    }
        return null;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值