我发了3次 就是没有出来JAVAMAIL.... BS CSDN

解决方案 »

  1.   

    如果你想偷懒的话,可以用apache的commons的发mail的libhttp://commons.apache.org/email/
      

  2.   

    package mail;import java.util.Date;import java.util.Properties; import javax.mail.Message;import javax.mail.MessagingException;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 EmailService {        // 默认主机;邮箱发送;下面的用户名和密码就是你sohu邮箱的username/pwd       private static final String host = "smtp.163.com";        // 是谁发的信息       private static final String fromEmail = "*******@163.com";        public static void main(String[] args) {              EmailService email = new EmailService();              email.sendMess(null, "*******@163.com", null, "标题", "内容");       }
           /**        * 客户端调用用于发送信息        *         * @param emailToOne:第一个要收信息的邮箱        * @param emailTwo:第二个要收信息的邮箱        * @param emailThree:第三个要收信息的邮箱        * @param subject:标题        * @param body:内容        */       public void sendMess(String emailToOne, String emailTwo, String emailThree,                     String subject, String body) {              send("*******@163.com", "", "", "测试", "中华人民共和国");       }        private void send(String emailToOne, String emailTwo, String emailThree,                     String subject, String body) {               Properties props = null;// 属性类              Session mailsession = null;// 在每一次发送邮件信息是要创建的一个session              Message msg = null;// 用于存储信息类              try {                     props = new Properties();                     props.put("mail.smtp.host", host);// 所使用的转移邮箱                     props.put("mail.smtp.auth", "true");// 必需的                     mailsession = Session.getDefaultInstance(props, SmtpAuth.getInstance());                     // true:显示运行时信息显示结果;false:不显示执行的结果信息                     mailsession.setDebug(false);                     /**                      * Message.RecipientType.TO                      *                       * Message.RecipientType.CC                      *                       * Message.RecipientType.BCC                      */                     // 使用每次session创建一个存储信息的类对象、说明此信息只在当前session范围内使用                     msg = new MimeMessage(mailsession);                     msg.setFrom(new InternetAddress(fromEmail));                     if (null != emailToOne || !emailToOne.equals("")) {                            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailToOne));                     } else if (null != emailTwo || !emailTwo.equals("")) {                            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(emailTwo));                     } else if (null != emailThree || !emailThree.equals("")) {                            msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(emailThree));                     }                     msg.setSentDate(new Date());// 发送的时间既当前时间                     msg.setSubject(subject);// 相当于标题                     msg.setText(body);// 设置内容                     msg.saveChanges();// 保存修改项                     Transport.send(msg);// 发送信息              } catch (AddressException e) {                     e.printStackTrace();              } catch (MessagingException e) {                     e.printStackTrace();              }       }}
    package mail;/**
     * 
     * 用于保存输入的用户名/密码
     * @author Administrator
     */class SmtpAuth extends javax.mail.Authenticator { private String user; private String password; private static SmtpAuth smtpAuth = null; // 返回当前类的对象; public static SmtpAuth getInstance() { if (null == smtpAuth) {
    smtpAuth = new SmtpAuth();
    } return smtpAuth;
    } // 使用默认构造函数使用默认制定用户名和密码 public SmtpAuth() {
    user = "*******@163.com";
    password = "********";// 将*改为你的真正密码
    } public void setUserinfo(String getuser, String getpassword) { user = getuser; password = getpassword; } protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(user, password);
    }
    }
    所用jar包:activation.jar,javamail.jar
      

  3.   

    不过你想要系统的学一下怎么用java 发送email 的话。
    建议你去看看张孝祥的一本专门讲javamail 的书
      

  4.   

    个人认为用apache的commons_mail比java mail要好,www.apache.org上把包下来下就可以了。用法比较简单……代码和7楼的类似
      

  5.   

    如何使用Jmail收发邮件
    http://blog.csdn.net/wpabbs/archive/2008/05/19/2458006.aspx
      

  6.   

    使用javamail发送邮件,接收邮件地址需要开通pop3协议,现在很多免费邮箱已经不支持免费的,需要交钱才开通,没开通的会导致你邮件发送失败
      

  7.   

    java mail例子:
    package mail.dao;import java.util.Date;
    import java.util.Properties;import javax.mail.*;
    import javax.mail.internet.*;import mail.domain.*;import mail.domain.MailBox;public class MailUtilDAOImp { private static String mailType = "text/html;charset=gb2312"; private Session sendMailSession = null; private MailBox fromMailBox; public MailBox getFromMailBox() {
    return fromMailBox;
    } public void setFromMailBox(MailBox fromMailBox) {
    this.fromMailBox = fromMailBox;
    } public Session getSendMailSession() {
    return sendMailSession;
    } public void setSendMailSession(Session sendMailSession) {
    this.sendMailSession = sendMailSession;
    } public void sendMail(String targetAddr, MailMessage mailmsg) {
    getSession(); Message newMessage = new MimeMessage(sendMailSession);
    try {
    newMessage
    .setFrom(new InternetAddress(fromMailBox.getMailBoxAddr()));
    InternetAddress[] to = new InternetAddress[1]; to[0] = new InternetAddress(targetAddr); // 接受方邮件地址 newMessage.setRecipients(Message.RecipientType.TO, to);// 接受方邮件地址
    // 取标题
    newMessage.setSubject(mailmsg.getSubject());
    // 取内容
    // newMessage.setContent(mailmsg.getContent(),""); //邮件正文
    // 不支持html,MimeMessage无setContent(String arg0)
    Multipart mp = new MimeMultipart(); // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
    MimeBodyPart mbp = new MimeBodyPart();
    mbp.setContent(mailmsg.getContent(), mailType);
    mp.addBodyPart(mbp);
    newMessage.setContent(mp);// 设置内容
    newMessage.setSentDate(new Date(System.currentTimeMillis())); Transport.send(newMessage);
    } catch (AddressException e) { e.printStackTrace();
    } catch (MessagingException e) { e.printStackTrace();
    } } public synchronized void sendMail(String[] targetAddrList,
    MailMessage mailmsg) { for (int i = 0; i < targetAddrList.length; i++) {
    try {
    sendMail(targetAddrList[i], mailmsg);
    } catch (Exception ex) { ex.printStackTrace();
    }
    } } /**
     * 获得一个会话 组装sendMailSession
     * 
     * @return
     */
    public Session getSession() {
    if (sendMailSession == null) {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", fromMailBox.getMailServAddr()); // smtp主机名。
    props.put("mail.smtp.user", fromMailBox.getUserAcct()); // smtp主机名。
    props.put("mail.smtp.port", fromMailBox.getMailPort());
    props.put("mail.smtp.password", fromMailBox.getUserPass()); // 邮件密码。
    PopupAuthenticator popA = new PopupAuthenticator(); // 邮件安全认证。
    popA
    .permCheck(fromMailBox.getUserAcct(), fromMailBox
    .getUserPass()); // 填写用户名及密码
    sendMailSession = Session.getInstance(props, popA);
    sendMailSession.setDebug(false);
    }
    return sendMailSession;
    } /**
     * 安全认证
     */
    public class PopupAuthenticator extends Authenticator {
    String username = null; String password = null; public PopupAuthenticator() {
    } public PasswordAuthentication permCheck(String username, String password) {
    this.username = username;
    this.password = password;
    return getPasswordAuthentication();
    } protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(username, password);
    }
    }
    }