简体   繁体   English

不能使用gmail-smtp和javamail发送电子邮件?

[英]Can't send email using gmail-smtp with javamail?

I'm using Javamail to sned the email from my gmail to others but the system couldn't send it. 我正在使用Javamail将gmail邮件发送给其他人,但系统无法发送。 It keeps on saying it couldn't load main class. 它一直说无法加载主类。

The code I got was a demo code so I think it's not the problem. 我得到的代码是演示代码,所以我认为这不是问题。 Anyhow, I'm posting it here. 无论如何,我在这里发布。

package Email;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailTLS {

    public static void main(String[] args) {

        final String username = "abc@gmail.com";
        final String password = "abc";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("abc@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("def@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

Now, I've set my CLASSPATH with all the jar in D:\\... and also the place where I store my .java file. 现在,我将所有jar都设置在CLASSPATH中,即D:\\...以及我存储.java文件的位置。 There's no space in the link. 链接中没有空格。 When I compiled, the program did it just fine. 当我编译时,该程序就很好了。 The problem started when I tried to run the code. 当我尝试运行代码时,问题开始了。

` `

PS D:\Documents\java> javac SendMailTLS.java
PS D:\Documents\java> java SendMailTLS
Error: Could not find or load main class SendMailTLS
PS D:\Documents\java>
PS D:\Documents\java> java Email.SendMailTLS
Error: Could not find or load main class Email.SendMailTLS
PS D:\Documents\java>
PS D:\Documents\java> java .Email.SendMailTLS
Error: Could not find or load main class .Email.SendMailTLS
PS D:\Documents\java>
PS D:\Documents\java> java -cp D:\Documents\java\ Email.SendMailTLS
Error: Could not find or load main class Email.SendMailTLS
PS D:\Documents\java>
PS D:\Documents\java> java -cp D:\Documents\java\ SendMailTLS
Error: Could not find or load main class SendMailTLS
PS D:\Documents\java>
`

Any idea why it's like this? 知道为什么会这样吗?

Thanks a lot in advance. 非常感谢。

Zestos. Zestos。

You declare your class to be in the package "Email". 您声明您的班级在“电子邮件”包中。 The class loader will search for this class in a subdirectory "Email", not in the current directory. 类加载器将在“ Email”子目录中而不是当前目录中搜索此类。 Create a directory "Email" in a directory of your classpath, or under the current directory, and move your class there. 在类路径的目录中或当前目录下创建目录“电子邮件”,然后将类移动到该目录中。 Then call the class as Email.SendMailTLS 然后将该类称为Email.SendMailTLS

Alternatively just remove the package statement from your class. 或者,只需从您的类中删除package语句。

BTW, naming convention is to keep the package name in lowercase. 顺便说一句,命名约定是将程序包名称保留为小写。

像这样将属性mail.smtp.starttls.enable的值更改为false:

props.put("mail.smtp.starttls.enable", "false"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM