/*
 * Created on 2004-4-26
 */import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;/**
 * @author Bromon
 */
public class SenderWithSMTPVer1 {
String host = ""; String user = ""; String password = ""; public void setHost(String host) {
this.host = host;
} public void setAccount(String user, String password) {
this.user = user;
this.password = password;
} public void send(String from, String to, String subject, String content) {
Properties props = new Properties();
props.put("mail.smtp.host", host);// 指定SMTP服务器
props.put("mail.smtp.auth", "true");// 指定是否需要SMTP验证
try {
Session mailSession = Session.getDefaultInstance(props); mailSession.setDebug(true);// 是否在控制台显示debug信息 Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));// 发件人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));// 收件人 message.setSubject(subject);// 邮件主题
message.setText(content);// 邮件内容
message.saveChanges(); Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
System.out.println(e);
} } public static void main(String args[]) {

SenderWithSMTPVer1 sm = new SenderWithSMTPVer1(); sm.setHost("smtp.126.com");// 指定要使用的邮件服务器
sm.setAccount("wendaoke", "12345647");// 指定帐号和密码 /*
 * @param String 发件人的地址 @param String 收件人地址 @param String 邮件标题 @param
 * String 邮件正文
 */
sm.send("[email protected]", "[email protected]", "标题", "内容");
}}
这是网上的一个例子,我在我机子上运行了,报错如下:
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.126.com", port 25220 126.com Anti-spam GT for Coremail System (126com[030901])
DEBUG SMTP: connected to host "smtp.126.com", port: 25EHLO deepin-d13f1cca
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 dXNlcm5hbWU6
d2VuZGFva2U=
334 UGFzc3dvcmQ6
MTM5NTE4ODIyNjQ=
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 Mail OK
RCPT TO:<[email protected]>
250 Mail OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354 Send from Rising mail proxy
java.lang.SecurityException: SHA1 digest error for com/sun/mail/util/LineOutputStream.class
at sun.security.util.ManifestEntryVerifier.verify(ManifestEntryVerifier.java:195)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:207)
at java.util.jar.JarVerifier.update(JarVerifier.java:194)
at java.util.jar.JarVerifier$VerifierStream.read(JarVerifier.java:382)
at sun.misc.Resource.getBytes(Resource.java:64)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1130)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1607)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:390)
at sendMail.main(sendMail.java:42)
Exception in thread "main"
主要的问题就是java.lang.SecurityException: SHA1 digest error for com/sun/mail/util/LineOutputStream.class
这个出错在哪里?搞了一天,也没有弄明白,大家帮帮忙,怎么解决?

解决方案 »

  1.   

    Session mailSession = Session.getDefaultInstance(props);
    这条语句前面添加
    PopupAuthenticator popAuthenticator = new PopupAuthenticator();
    PasswordAuthentication pop = popAuthenticator.performCheck("wendaoke","1234567"); 
    你试一下
      

  2.   

    public class Untitled1 {
      static String smtphost = "smtp.126.com"; //Domino smtphost,you should make sure the smtp service is opened in domino Server
      static String user = "";
      static String password = "";
      static String from = "[email protected]";
      static String to = "[email protected]";
      static String subject = "Java Mail Subject!";
      static String body = "Java Mail Body!";
    // the following is mail sending program,user needn't to revise it  public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", smtphost);
        props.put("mail.smtp.auth", "true");    try {
          SMTPAutherticator sa = new SMTPAutherticator("wendaoke", "12345678");
          Session ssn = Session.getInstance(props, sa);
          MimeMessage message = new MimeMessage(ssn);
          InternetAddress fromAddress = new InternetAddress(from);
          message.setFrom(fromAddress);
          InternetAddress toAddress = new InternetAddress(to);
          message.addRecipient(Message.RecipientType.TO, toAddress);
          message.setSubject("subject");
          message.setText("body");
          Transport transport = ssn.getTransport("smtp");
          transport.connect(smtphost, user, password);
          transport.sendMessage(message,
                                message.getRecipients(Message.RecipientType.TO));
          transport.send(message);
          transport.close();
        }
        catch (MessagingException ex) {
          ex.printStackTrace();
        }
      }
    }
    class SMTPAutherticator
        extends Authenticator {
      private String userName = null;
      private String userPass = null;  public SMTPAutherticator(String userName, String userPass) {
        super();
        this.userName = userName;
        this.userPass = userPass;
      }
      public PasswordAuthentication getPasswordAuthentication() {    return new PasswordAuthentication(userName, userPass);
      }
    }
    按照你的例子大概改了一下,你测试一下吧.自己组织一下代码美观和结构.
      

  3.   

    谢谢各位了,我在原来JDK里发现有mail.jar,aviation.jar,把他们删了,就可以执行了