zoukankan      html  css  js  c++  java
  • java发送邮件完整实例 java邮件工具类

    http://yuncode.net/code/c_552a2e2dc593894

    package com.srie.mail;
    
    import java.util.Properties;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class SendmailUtil {
        
        // 设置服务器
        private static String KEY_SMTP = "mail.smtp.host";
        private static String VALUE_SMTP = "10.1.4.125";
        // 服务器验证
        private static String KEY_PROPS = "mail.smtp.auth";
        private static boolean VALUE_PROPS = true;
        // 发件人用户名、密码
        private String SEND_USER = "wangshengdong@itc.cr";
        private String SEND_UNAME = "wangshengdong";
        private String SEND_PWD = "a";
        // 建立会话
        private MimeMessage message;
        private Session s;
     
        /*
         * 初始化方法
         */
        public SendmailUtil() {
            Properties props = System.getProperties();
            props.setProperty(KEY_SMTP, VALUE_SMTP);
            props.put(KEY_PROPS, "true");
            //props.put("mail.smtp.auth", "true");
            s =  Session.getDefaultInstance(props, new Authenticator(){
                  protected PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
                  }});
            s.setDebug(true);
            message = new MimeMessage(s);
        }
     
        /**
         * 发送邮件
         * 
         * @param headName
         *            邮件头文件名
         * @param sendHtml
         *            邮件内容
         * @param receiveUser
         *            收件人地址
         */
        public void doSendHtmlEmail(String headName, String sendHtml,
                String receiveUser) {
            try {
                // 发件人
                InternetAddress from = new InternetAddress(SEND_USER);
                message.setFrom(from);
                // 收件人
                InternetAddress to = new InternetAddress(receiveUser);
                message.setRecipient(Message.RecipientType.TO, to);
                // 邮件标题
                message.setSubject(headName);
                String content = sendHtml.toString();
                // 邮件内容,也可以使纯文本"text/plain"
                message.setContent(content, "text/html;charset=GBK");
                message.saveChanges();
                Transport transport = s.getTransport("smtp");
                // smtp验证,就是你用来发邮件的邮箱用户名密码
                transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
                // 发送
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
                System.out.println("send success!");
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
     
        public static void main(String[] args) {
            SendmailUtil se = new SendmailUtil();
            se.doSendHtmlEmail("邮件头文件名", "邮件内容", "wangshengdong@itc.cr");
        }
    
    }
  • 相关阅读:
    01_javaSE面试题:自增变量
    SpringBoot(二十)_404返回统一异常处理结果
    MD5加密工具代码
    SpringBoot(十九)_spring.profiles.active=@profiles.active@ 的使用
    读取本地文件转化成MultipartFile
    remote: http basic: access denied fatal: authentication failed for '‘解决办法
    git报错_you are not allowed to push code to protected branches on this project
    SpringBoot(十八)_springboot打成war包部署
    sql优化问题笔记(mysql)
    gitbook 简单使用
  • 原文地址:https://www.cnblogs.com/stono/p/6170715.html
Copyright © 2011-2022 走看看