Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 发送邮件代码 #6

Open
muyuqiu001 opened this issue Nov 8, 2017 · 1 comment
Open

Java 发送邮件代码 #6

muyuqiu001 opened this issue Nov 8, 2017 · 1 comment

Comments

@muyuqiu001
Copy link
Owner

No description provided.

@muyuqiu001
Copy link
Owner Author



import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtil
{
    /*
     * 系统邮箱账户(配置文件)
     */
    public static String myEmailAccount = "";

    /*
     * 邮箱密码(客户端授权密码)
     */
    public static String myEmailPassword = "";

    /**
     * smtp服务地址 qq邮箱是smtp.qq.com
     */
    public static String myEmailSMTPHost = "smtp.139.com";

    /*
     * smtp服务端口 qq是465
     */
    public static final String smtpPort = "465";

    /**
     * 
     * @param receiveMail
     *            收件人
     * @param theme
     *            邮件主题
     * @param content
     *            邮件内容
     */
    public static void sendMail(String receiveMail, String theme, String content)
    {
        Properties props = new Properties(); // 参数配置
        props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
        props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.auth", "true");

        props.setProperty("mail.smtp.port", smtpPort);
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.socketFactory.port", smtpPort);

        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);

        // 1. 创建一封邮件
        MimeMessage message = new MimeMessage(session);

        try
        {
            // 2. From: 发件人
            message.setFrom(new InternetAddress(myEmailAccount, "招投标分析平台", "UTF-8"));

            // 3. To: 收件人(可以增加多个收件人、抄送、密送)
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, null, "UTF-8"));

            // 4. Subject: 邮件主题
            message.setSubject(theme, "UTF-8");

            // 5. Content: 邮件正文(可以使用html标签)
            message.setContent(content, "text/html;charset=UTF-8");

            // 6. 设置发件时间
            message.setSentDate(new Date());

            // 7. 保存设置
            message.saveChanges();

            Transport transport = session.getTransport();
            transport.connect(myEmailAccount, myEmailPassword);

            // 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人,
            // 密送人
            transport.sendMessage(message, message.getAllRecipients());

            // 7. 关闭连接
            transport.close();
        }
        catch (UnsupportedEncodingException e)
        {
           
        }
        catch (MessagingException e)
        {
            
        }

    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant