package com.wjg.email;import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.util.*;/**
 * @author wjg
 *
 * 发送邮件简单类
 */
public class SendMail {
    String host; //smtp服务器地址    String from; //发送邮箱地址    String username = null; //smtp用户名    String password = null; //smtp用户密码    public void setHost(String host) {
        this.host = host;
    }    public String getHost() {
        return this.host;
    }    public void setFrom(String from) {
        this.from = from;
    }    public String getFrom() {
        return this.from;
    }    public void setUsername(String username) {
        this.username = username;
    }    public String getUsername() {
        return this.username;
    }    public void setPassword(String password) {
        this.password = password;
    }    public String getPassword() {
        return this.password;
    }
    /**
     * 发送邮件到指定邮箱;调用前要设置host属性,
     * 需要验证则设置用户名和密码,不支持附件
     * 
     * @param sendto 发送目标邮箱
     * @param subject //邮件主题
     * @param content //邮件内容,可以含html
     */
    public void sendMail(String sendto, String subject, String content) {
        Authenticator mailAuth = null;
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        if (this.username != null) {
            mailAuth = new EmailAuthenticator(username, password);
            props.put("mail.smtp.auth", "true");
        }
        Session session = Session.getInstance(props, mailAuth);        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(
                    sendto));
            message.setSubject(subject);
            message.setSentDate(new Date());
            message.setContent(content, "text/html");
            message.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect(host, username, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }    }
};

解决方案 »

  1.   

    package com.wjg.email;import javax.mail.Authenticator;
    import javax.mail.*;public class EmailAuthenticator
        extends Authenticator {  String username = null;
      String password = null;  public EmailAuthenticator() {
      super();
      }
      public EmailAuthenticator(String username,String password) {
      super();
      this.username=username;
      this.password=password;
      }
      public PasswordAuthentication performCheck(String user, String pass) {
        username = user;
        password = pass;
        return getPasswordAuthentication();
      }  protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
      }}
      

  2.   

    刚刚试了,不行啊。返回 class javax.mail.SendFailedException:550 5.7.1 Unable to relay for [email protected]
       at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:926)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:389)
    at com.gamemoney.web.email.SendMail.sendout(SendMail.java:99)
    at com.gamemoney.web.email.SendMail.main(SendMail.java:124)和我用 foxmail 发送邮件返回的错误一样。。我怀疑这个是一个密码加密认证。。
      

  3.   

    看到你是用21CN.com信箱发送的,我就改了一下我的程序.
    给你我刚才测试通过的程序.
    密码没有加密过.import java.util.Properties;
    import java.util.Date;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    public class Test7 { public static void main(String[] args) throws Exception {
    sendhtml(args);
    } public static void sendhtml(String[] argv) throws Exception { String to = "[email protected]",
    subject = "null",
    from = "[email protected]",
    cc = null,
    bcc = null;
    String mailhost = "smtp.21cn.com";
    String username = "XX";
    String password = "XX"; boolean debug = false;
    Properties props = System.getProperties();
    props.put("mail.smtp.auth", "true");
    SMTPAuth auth = new SMTPAuth(username, password);
    if (mailhost != null)
    props.put("mail.smtp.host", mailhost); // Get a Session object
    Session session = Session.getDefaultInstance(props, auth);
    if (debug)
    session.setDebug(true); // construct the message
    Message msg = new MimeMessage(session);
    if (from != null)
    msg.setFrom(new InternetAddress(from));
    else
    msg.setFrom(); msg.setRecipients(
    Message.RecipientType.TO,
    InternetAddress.parse(to, false));
    if (cc != null)
    msg.setRecipients(
    Message.RecipientType.CC,
    InternetAddress.parse(cc, false));
    if (bcc != null)
    msg.setRecipients(
    Message.RecipientType.BCC,
    InternetAddress.parse(bcc, false)); subject = new Date().toLocaleString();
    msg.setSubject(subject); MimeBodyPart mbp1 = new MimeBodyPart();
    String html =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
    + "<html>"
    + "<head><title></title></head>"
    + "<body>"
    + "<b> see the following jpg : it is a car!</b><br/>"
    + "<a href=http://www.a.com/a.jsp>hello</a><br/>"
    + "<IMG src=cid:7e2a34e1.jpg><br/>"
    + "<b> end of jpg</b>"
    + "</body>"
    + "</html>"; mbp1.setContent(html, "text/html"); FileDataSource fds = new FileDataSource("c:/7e2a34e1.jpg");
    MimeBodyPart mbp2 = new MimeBodyPart();

    mbp2.setFileName(fds.getName());
    mbp2.setText("This is a beautiful car !");
    mbp2.setDataHandler(new DataHandler(fds));
    mbp2.setHeader("Content-ID", "<" + fds.getName() + ">");
    mbp2.setDisposition(null); MimeMultipart mp = new MimeMultipart("related"); //alternative
    mp.addBodyPart(mbp1);
    mp.addBodyPart(mbp2);
    msg.setContent(mp); msg.setSentDate(new Date());
    Transport.send(msg);
    System.out.println(mp.getCount());
    System.out.println("\nMail was sent successfully."); }}
    class SMTPAuth extends javax.mail.Authenticator {
    private String user, password;
    public SMTPAuth(String u, String p) {
    user = u;
    password = p;
    }
    public void getuserinfo(String getuser, String getpassword) {
    user = getuser;
    password = getpassword;
    }
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
    return new javax.mail.PasswordAuthentication(user, password);
    }}