java 发email

package com.hua.test;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * <p>Title: java mail</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2011</p>
 * <p>Company: </p>
 * @author hua
 * @version 1.0
 */

public class SendMail
{

    public SendMail()
    {
    }
    public void send(){
        try
        {
            Properties props = new Properties();
            Session sendMailSession;
            Store store;
            Transport transport;
            props.put("mail.smtp.auth","true");
            props.put("mail.smtp.host", "smtp.sina.com"); //smtp主机名。
            props.put("mail.smtp.user","[email protected]"); //发送方邮件地址。
            props.put("mail.smtp.password","XXXXXX"); //邮件密码。
            PopupAuthenticator popA=new PopupAuthenticator();//邮件安全认证。
            PasswordAuthentication pop = popA.performCheck("doloveme70910054","XXXXXX"); //填写用户名及密码
            sendMailSession = Session.getInstance(props, popA);
            Message newMessage = new MimeMessage(sendMailSession);
            newMessage.setFrom(new InternetAddress("[email protected]"));
            newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));  //接收方邮件地址
            newMessage.setSubject("邮件主题");
            newMessage.setSentDate(new Date());
            String mailContent;
            mailContent="你好!\n\n";
            mailContent+="\t邮件正文\n\n";
            mailContent+=new Date().toLocaleString();
            newMessage.setText(mailContent); //邮件正文
            transport = sendMailSession.getTransport("smtp");
            transport.send(newMessage);
        }
        catch (MessagingException ex)
        {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args)
    {
        SendMail sml = new SendMail();
        sml.send();
    }

    public class PopupAuthenticator extends Authenticator{
        String username=null;
        String password=null;
        public PopupAuthenticator(){}
        public PasswordAuthentication performCheck(String user,String pass){
            username = user;
            password = pass;
            return getPasswordAuthentication();
        }
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }

    }
}


 

你可能感兴趣的:(java,C++,c,C#)