java 发送邮件

-新建工具类

package test;

import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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;

public class GServerMail
{
    public static String MAIL_FROM = "123@163.com"; //发件人邮箱
    public static String MAIL_SERVER = "smtp.163.com";  //  SMTP服务器端口号(端口25)
    public static String MAIL_PROTOCOL = "smtp";
    public static String MAIL_PORT = "25";  //端口号
    public static String MAIL_USER = "123@163.com"; //  发件人邮箱
    public static String MAIL_PASSWORD = "******";  //邮箱密码
    public static String DISPLAYNAME = "邮件名称";
    public static boolean NEEDAUTH = true;


     public GServerMail()
     {
     }

     public static void setMailServer(String host,String port, String mailbox,String user,String passwd,String displayName,boolean needAuth)
     {
         MAIL_SERVER = host;
         MAIL_PORT = port;
         MAIL_FROM = mailbox;
         MAIL_USER = user;
         MAIL_PASSWORD = passwd;
         DISPLAYNAME = displayName;
         NEEDAUTH = needAuth;
     }

     public static boolean sendHtmlMsg(String to,String subject,String content)
     {
        try
      {
         Properties props = new Properties();
       props.put("mail.smtp.host",MAIL_SERVER);
       props.put("mail.smtp.port",MAIL_PORT);
       props.put("mail.smtp.localhost", "localhost");

       AuthenticatorImple auth = null;
       if(NEEDAUTH)
       {
           props.put("mail.smtp.auth","true");
           auth = new AuthenticatorImple(MAIL_USER,MAIL_PASSWORD);
       }
       else
       {
           props.put("mail.smtp.auth","false");
       }
       Session session = Session.getInstance(props,auth);
       Message msg = new MimeMessage(session);
       msg.setFrom(new InternetAddress(MAIL_FROM,DISPLAYNAME));
       msg.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
       msg.setSubject("=?GB2312?B?"+Base64.encode(subject.getBytes("GB2312"))+"?=");
       msg.setSentDate(new Date());

           // MiniMultipart是消息容器
           Multipart mainPart = new MimeMultipart();
           //装载HTML内容
           BodyPart html = new MimeBodyPart();
           //html.setContent(content, "text/html; charset=GB2312");
           html.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
                       + content, "text/html;charset=GB2312");
           mainPart.addBodyPart(html);
           msg.setContent(mainPart);

           Transport transport = session.getTransport("smtp");
           transport.connect(MAIL_SERVER, MAIL_USER,MAIL_PASSWORD);
           transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
           System.out.println("send mail successed");
           transport.close();
           return true;
      }
        catch(Exception ex)
      {
          ex.printStackTrace();
       }
         return false;
     }


    public static void main(String[] args)
    {
        //JMail mail = new JMail();
      GServerMail.sendHtmlMsg("发送人", "主题", "邮件内容");
    }
}

-依赖AuthenticatorImple类

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class AuthenticatorImple extends Authenticator
{
    String userName=null;
    String password=null;
    public AuthenticatorImple()
    {
    }
    public AuthenticatorImple(String username, String password)
    {
       this.userName = username;
       this.password = password;
    }
    protected PasswordAuthentication getPasswordAuthentication()
   {
     return new PasswordAuthentication(userName, password);
    }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值