Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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
Java 使用servlet发送邮件时在netBeans中显示未知主机异常_Java_Email_Servlets_Netbeans - Fatal编程技术网

Java 使用servlet发送邮件时在netBeans中显示未知主机异常

Java 使用servlet发送邮件时在netBeans中显示未知主机异常,java,email,servlets,netbeans,Java,Email,Servlets,Netbeans,这是我的文件: index.html <form action="servlet/SendMail"> To:<input type="text" name="to"/><br/> Subject:<input type="text" name="subject"><br/> Text:<textarea rows="10" cols="70" name="msg"></textarea><br/>

这是我的文件:

index.html

<form action="servlet/SendMail">
To:<input type="text" name="to"/><br/>
Subject:<input type="text" name="subject"><br/>
Text:<textarea rows="10" cols="70" name="msg"></textarea><br/>
<input type="submit" value="send">
</form>
java

import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

public class Mailer {
public static void send(String to,String subject,String msg){

final String user="myid@gmail.com";

final String pass="*********";

Properties props = new Properties();

  props.setProperty("mail.smtp.port", "465");

    props.put("mail.transport.protocol", "smtp");

        props.setProperty("mail.smtp.socketFactory.port", "465");

        props.put("mail.smtp.socketFactory.class", 
"javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.host", "smtp.gmail.com");

        props.setProperty("mail.smtp.starttls.enable", "true");

        props.setProperty("mail.smtp.auth", "true");


Session session = Session.getInstance(props,

 new javax.mail.Authenticator() {

  protected PasswordAuthentication getPasswordAuthentication() {

   return new PasswordAuthentication(user,pass);

   }
});

try {
 MimeMessage message = new MimeMessage(session);

 message.setFrom(new InternetAddress(user));

 message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

 message.setSubject(subject);

 message.setText(msg);

 Transport.send(message);

 System.out.println("Done");

 } catch (MessagingException e) {

    throw new RuntimeException(e);
 }

}
}

你从哪里得到错误?有线索吗?类似于线程“main”java.lang.RuntimeException中的异常:javax.mail.MessaginException:无法连接到SMTP主机:SMTP.gmail.com,端口:587??需要对代码进行一些调试。运行时是否连接了internet。是的,我连接了internet@Satya。@sheetal Mohan sharma我真的不知道哪里出了问题。
import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

public class Mailer {
public static void send(String to,String subject,String msg){

final String user="myid@gmail.com";

final String pass="*********";

Properties props = new Properties();

  props.setProperty("mail.smtp.port", "465");

    props.put("mail.transport.protocol", "smtp");

        props.setProperty("mail.smtp.socketFactory.port", "465");

        props.put("mail.smtp.socketFactory.class", 
"javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.host", "smtp.gmail.com");

        props.setProperty("mail.smtp.starttls.enable", "true");

        props.setProperty("mail.smtp.auth", "true");


Session session = Session.getInstance(props,

 new javax.mail.Authenticator() {

  protected PasswordAuthentication getPasswordAuthentication() {

   return new PasswordAuthentication(user,pass);

   }
});

try {
 MimeMessage message = new MimeMessage(session);

 message.setFrom(new InternetAddress(user));

 message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

 message.setSubject(subject);

 message.setText(msg);

 Transport.send(message);

 System.out.println("Done");

 } catch (MessagingException e) {

    throw new RuntimeException(e);
 }

}
}