用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

Email

2018-05-26 作者: 云代码会员举报

[java]代码库

package Email;


import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class Main {

    
    public static String myEmailAccount = "jinzhenyu19981110@163.com";
    public static String myEmailPassword = "JINZHENYU330225";


    public static String myEmailSMTPHost = "smtp.163.com";


    public static String receiveMailAccount = "87944714@qq.com";

    public static void main(String[] args) throws Exception {
   
        Properties props = new Properties();                   
        props.setProperty("mail.transport.protocol", "smtp");   
        props.setProperty("mail.smtp.host", myEmailSMTPHost);   
        props.setProperty("mail.smtp.auth", "true");           


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

        MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);


        Transport transport = session.getTransport();


        transport.connect(myEmailAccount, myEmailPassword);

       
        transport.sendMessage(message, message.getAllRecipients());

  
        transport.close();
    }

    
    public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {
        // 1. 创建一封邮件
        MimeMessage message = new MimeMessage(session);

        // 2. From: 发件人(昵称有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改昵称)
        message.setFrom(new InternetAddress(sendMail, "小情人", "UTF-8"));

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

        // 4. Subject: 邮件主题(标题有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改标题)
        message.setSubject("情书", "UTF-8");

        // 5. Content: 邮件正文(可以使用html标签)
        message.setContent("开心小金,你好,我是你的崇拜者,暗恋了你10年", "text/html;charset=UTF-8");

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

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

        return message;
    }

}

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...