用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

java发送邮件完整实例 java邮件工具类

2015-04-12 作者: java源代码大全举报

[java]代码库

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 = "smtp.qq.com";
    // 服务器验证
    private static String KEY_PROPS = "mail.smtp.auth";
    private static boolean VALUE_PROPS = true;
    // 发件人用户名、密码
    private String SEND_USER = "2569000943@qq.com";
    private String SEND_UNAME = "2569000943";
    private String SEND_PWD = "********";
    // 建立会话
    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("邮件头文件名""邮件内容""798210413@qq.com");
    }
}//源代码片段来自云代码http://yuncode.net
             
 
import java.security.Security;
import java.util.Date;
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 sendEmail {
    public static void main(String[] args) throws AddressException,
            MessagingException {
        String SEND_USER = "2569000943@qq.com";
        String SEND_UNAME = "2569000943";
        String SEND_PWD = "********";
        String VALUE_SMTP = "smtp.qq.com";
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        //final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // Get a Properties object
        Properties props = System.getProperties();
        // props.setProperty("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.host""smtp.qq.com");
        //props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        //props.setProperty("mail.smtp.socketFactory.fallback", "false");
         props.setProperty("mail.smtp.port""25");
        //props.setProperty("mail.smtp.port", "587");
        //props.setProperty("mail.smtp.socketFactory.port", "25");
        //props.setProperty("mail.smtp.socketFactory.port", "587");
        props.put("mail.smtp.auth""true");
        final String username = "2569000943";
        final String password = "chHorse123";
        Session session = Session.getDefaultInstance(props,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
 
        // -- Create a new message --
        session.setDebug(true);
        Message msg = new MimeMessage(session);
 
        // -- Set the FROM and TO fields --
        msg.setFrom(new InternetAddress(username + "@qq.com"));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
                "798210413@qq.com"false));
        msg.setSubject("Hello---");
        msg.setText("How are you");
        msg.setSentDate(new Date());
        Transport transport = session.getTransport("smtp");
        // smtp验证,就是你用来发邮件的邮箱用户名密码
        transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
        // 发送
        transport.sendMessage(msg, msg.getAllRecipients());
        Transport.send(msg);
        transport.close();
 
        System.out.println("Message sent.");
    }
 
}//源代码片段来自云代码http://yuncode.net
            


网友评论    (发表评论)


发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...