Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
邮件发送问题 公共类MailEx{ 公共静态void main(字符串[]args){ 试一试{ 字符串用户名=”abc@gmail.com"; 字符串密码=“123”; 字符串hostName=“smtp.gmail.com”; 字符串fromName=“Splendore Bkk”; 字符串到[]={”xyz@gmail.com"}; System.out.println(“to.length::”+to.length); Properties props=新属性(); 字符串SSL_FACTORY=“javax.net.SSL.SSLSocketFactory”; put(“mail.smtp.host”、“smtp.gmail.com”); props.put(“mail.smtp.port”,“465”); props.put(“mail.debug”,“true”); props.put(“mail.smtp.auth”,“true”); props.put(“mail.smtp.starttls.enable”、“true”); setProperty(“mail.smtp.socketFactory.port”,“465”); setProperty(“mail.smtp.socketFactory.class”,SSL_工厂); setProperty(“mail.smtp.socketFactory.fallback”、“false”); System.out.println(“to.length:sadfsadfds:+to.length”); //获取默认会话对象。 Session=Session.getInstance(props); //创建默认的mimessage对象。 MimeMessage message1=新MimeMessage(会话); //使用 //InternetAddress.getLocalAddress方法的值。 message1.setFrom(新的InternetAddress(用户名,fromName)); 地址[]地址=新地址[至.length]; for(int i=0;i_Java_Jakarta Mail - Fatal编程技术网

邮件发送问题 公共类MailEx{ 公共静态void main(字符串[]args){ 试一试{ 字符串用户名=”abc@gmail.com"; 字符串密码=“123”; 字符串hostName=“smtp.gmail.com”; 字符串fromName=“Splendore Bkk”; 字符串到[]={”xyz@gmail.com"}; System.out.println(“to.length::”+to.length); Properties props=新属性(); 字符串SSL_FACTORY=“javax.net.SSL.SSLSocketFactory”; put(“mail.smtp.host”、“smtp.gmail.com”); props.put(“mail.smtp.port”,“465”); props.put(“mail.debug”,“true”); props.put(“mail.smtp.auth”,“true”); props.put(“mail.smtp.starttls.enable”、“true”); setProperty(“mail.smtp.socketFactory.port”,“465”); setProperty(“mail.smtp.socketFactory.class”,SSL_工厂); setProperty(“mail.smtp.socketFactory.fallback”、“false”); System.out.println(“to.length:sadfsadfds:+to.length”); //获取默认会话对象。 Session=Session.getInstance(props); //创建默认的mimessage对象。 MimeMessage message1=新MimeMessage(会话); //使用 //InternetAddress.getLocalAddress方法的值。 message1.setFrom(新的InternetAddress(用户名,fromName)); 地址[]地址=新地址[至.length]; for(int i=0;i

邮件发送问题 公共类MailEx{ 公共静态void main(字符串[]args){ 试一试{ 字符串用户名=”abc@gmail.com"; 字符串密码=“123”; 字符串hostName=“smtp.gmail.com”; 字符串fromName=“Splendore Bkk”; 字符串到[]={”xyz@gmail.com"}; System.out.println(“to.length::”+to.length); Properties props=新属性(); 字符串SSL_FACTORY=“javax.net.SSL.SSLSocketFactory”; put(“mail.smtp.host”、“smtp.gmail.com”); props.put(“mail.smtp.port”,“465”); props.put(“mail.debug”,“true”); props.put(“mail.smtp.auth”,“true”); props.put(“mail.smtp.starttls.enable”、“true”); setProperty(“mail.smtp.socketFactory.port”,“465”); setProperty(“mail.smtp.socketFactory.class”,SSL_工厂); setProperty(“mail.smtp.socketFactory.fallback”、“false”); System.out.println(“to.length:sadfsadfds:+to.length”); //获取默认会话对象。 Session=Session.getInstance(props); //创建默认的mimessage对象。 MimeMessage message1=新MimeMessage(会话); //使用 //InternetAddress.getLocalAddress方法的值。 message1.setFrom(新的InternetAddress(用户名,fromName)); 地址[]地址=新地址[至.length]; for(int i=0;i,java,jakarta-mail,Java,Jakarta Mail,我得到了错误 public class MailEx { public static void main(String[] args) { try { String userName = "abc@gmail.com"; String password = "123"; String hostName = "smtp.gmail.com"; String fromName = "

我得到了错误

public class MailEx {
public static void main(String[] args) {
    try {

        String userName = "abc@gmail.com";          
        String password = "123";            
        String hostName = "smtp.gmail.com";
        String fromName = "Splendore Bkk";
        String to[] = {"xyz@gmail.com"};

        System.out.println("to.length::"+to.length);

        Properties props = new Properties();
        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 

        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");     
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        System.out.println("to.length:sadfsadfds:"+to.length);
        // Get the default Session object.
        Session session = Session.getInstance(props);

        // Create a default MimeMessage object.
        MimeMessage message1 = new MimeMessage(session);

        // Set the RFC 822 "From" header field using the
        // value of the InternetAddress.getLocalAddress method.
        message1.setFrom(new InternetAddress(userName,fromName));

        Address[] addresses = new Address[to.length];
        for (int i = 0; i < to.length; i++) {
            Address address = new InternetAddress(to[i]);               
            addresses[i] = address;
            // Add the given addresses to the specified recipient type.
            message1.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
        }       
        // Set the "Subject" header field.
        message1.setSubject("Testing");

        // Sets the given String as this part's content,
        // with a MIME type of "text/plain".
        Multipart mp = new MimeMultipart("alternative");
        MimeBodyPart mbp = new MimeBodyPart();
        mbp.setContent("Hii from cc", "text/html");
        mp.addBodyPart(mbp);
        message1.setContent(mp);
        message1.saveChanges();

        // Send message
        Transport transport = session.getTransport("smtp");
        transport.connect(hostName,userName,password);
        transport.sendMessage(message1,addresses);
        transport.close();


    }catch (Exception e) {
        e.printStackTrace();
    }

}

}
调试:JavaMail版本1.4ea 调试:java.io.FileNotFoundException:..\java\jdk1.6.0\jre\lib\javamail.providers(系统找不到指定的文件) 调试:!任意加载 调试:未加载资源:/META-INF/javamail.providers 线程“main”java.lang.NoClassDefFoundError中出现异常:com/sun/mail/util/LineInputStream 位于javax.mail.Session.loadProvidersFromStream(Session.java:928) 在javax.mail.Session.access$000(Session.java:174) 在javax.mail.Session$1.load(Session.java:870) 在javax.mail.Session.loadResource(Session.java:1084) 位于javax.mail.Session.loadProviders(Session.java:889) 在javax.mail.Session.(Session.java:210) 位于javax.mail.Session.getInstance(Session.java:249) MailEx.main(MailEx.java:41)
那么你能告诉我问题出在哪里吗…

为了避免
调试
警告,请在下创建文件
javamail.providers、javamail.address.map、javamail.default.address.map、javamail.default.providers

DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: ..\Java\jdk1.6.0\jre\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
    at javax.mail.Session.loadProvidersFromStream(Session.java:928)
    at javax.mail.Session.access$000(Session.java:174)
    at javax.mail.Session$1.load(Session.java:870)
    at javax.mail.Session.loadResource(Session.java:1084)
    at javax.mail.Session.loadProviders(Session.java:889)
    at javax.mail.Session.<init>(Session.java:210)
    at javax.mail.Session.getInstance(Session.java:249)
    at com.test.MailEx.main(MailEx.java:41)
文件夹


关于错误,
NoClassDefFoundError
,您只是没有将JavaMail添加到类路径中。如果您使用的是Eclipse,右键单击project,遵循构建路径≥ 添加库或类似的东西,并将javamail的
jar
文件(应该位于
lib/
文件夹下)添加到项目的类路径中。

查看此解决方案

这是为您的问题提供理想的解决方案


希望这会有所帮助。

我修复了它,看看我的代码是如何编写的

必须在您的google帐户中进行审批 设置允许不太安全的应用打开 示例我是如何做到的
使用javax.mail版本1.4.7

 (Program Files)\Java\jdk1.6.0\jre\lib\

谢谢你的ans,但我已经添加了2个用于发送邮件的罐子。。i、 e mail.jar和activation.jar
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 void SendEmail() throws Exception {
String EmailUsername = "MYMAIL@gmail.com";
String PasswordUsername = "SomePassword";

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

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

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(EmailUsername));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("Example@gmail.com"));
        message.setSubject("This is the Qa test!");
        message.setText("this is the report");
        Transport.send(message);

        System.out.println("Message Sent!");

    } catch (MessagingException mex) {
        mex.printStackTrace();
    }
}