我正在尝试从java servlet向gmail发送自动邮件。我使用了以下类。
UITableView
我有一个servlet,我试图在一个类EmailTest中调用函数sendEmail。
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class EmailTest {
public void sendEmail(){
String to="toid@gmail.com";
String host="mail.javatpoint.com";
final String user="fromid@gmail.com";
final String password="password";
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
System.out.println("here");
Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//Compose the message
System.out.println("here");
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("javatpoint");
message.setText("This is simple program of sending email using JavaMail API");
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
但问题是它没有显示任何错误,也没有发送任何邮件。 我该如何解决这个问题?