Java邮件发送

package com.company.email;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email {

	private static final String TYPE = "text/html;charset=UTF-8";
	private static final String MAIL_SMTP_AUTH = "true";
	private static final String MAIL_SMTP_HOST = "smtp.163.com";

	private String username;
	private String password;

	public Email(String username, String password) {
		this.username = username;
		this.password = password;
	}

	public static void main(String[] args) throws Exception {

		Email email = new Email("zhangsan@163.com", "123456");
		email.sendTo("lisi@sina.com", "wangwu@qq.com", "hello", "How are you?");

	}

	public void sendTo(String receiver, String ccReceiver, String subject,
			String content) throws Exception {

		MimeMessage message = createMimeMessage(username, password);

		message.setFrom(new InternetAddress(username));
		message.setRecipient(RecipientType.TO, new InternetAddress(receiver));
		message.setRecipient(RecipientType.CC, new InternetAddress(ccReceiver));

		message.setSubject(subject);
		message.setContent(content, TYPE);

		Transport.send(message);
	}

	public static MimeMessage createMimeMessage(String username, String password) {

		Properties properties = createProperties(username, password);
		Authenticator authenticator = createAuthenticator(username, password);

		return new MimeMessage(Session.getInstance(properties, authenticator));
	}

	public static Properties createProperties(String username, String password) {
		Properties properties = new Properties();
		properties.put("mail.smtp.auth", MAIL_SMTP_AUTH);
		properties.put("mail.smtp.host", MAIL_SMTP_HOST);

		return properties;
	}

	public static Authenticator createAuthenticator(final String username,
			final String password) {

		return new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		};
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}



}

pom.xml 中依赖: 

<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.4.7</version>
</dependency>

linux环境

public class EmailUtils {

    private static final String TYPE = "text/html;charset=UTF-8";
    private static final String TRUE = "true";
    private static final String MAIL_SMTP_HOST = "smtp.163.com";
    private static final String MAIL_SMTP_PORT = "465";

    private static String username = "xxxxxx@163.com";
    private static String password = "xxxxx"; //授权码,非密码

    public static void sendTo(String subject, String content, String receiver, String... ccReceivers) throws Exception {
        if (StringUtils.isEmpty(content)) {
            return;
        }

        System.out.println("邮件内容:" + content);
        MimeMessage message = createMimeMessage(username, password);

        message.setFrom(new InternetAddress(username));
        message.setRecipient(RecipientType.TO, new InternetAddress(receiver));

        for (String ccReceiver : ccReceivers) {
            message.addRecipients(RecipientType.CC, ccReceiver);
        }

        message.setSubject(subject);
        message.setContent(content, TYPE);

        Transport.send(message);
    }

    public static MimeMessage createMimeMessage(String username, String password) {
        return new MimeMessage(Session.getInstance(createProperties(), createAuthenticator(username, password)));
    }

    public static Properties createProperties() {
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", TRUE);
        properties.put("mail.smtp.host", MAIL_SMTP_HOST);
        properties.put("mail.smtp.port", MAIL_SMTP_PORT); //更改端口号25为465,然后设置SSL协议
        properties.put("mail.smtp.ssl.enable", TRUE); //打开SSL协议
        // properties.put("mail.debug", TRUE);
        return properties;
    }

    public static Authenticator createAuthenticator(final String username, final String password) {
        return new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        };
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值